├── .gitignore
├── Base64.UnitTests
├── Base64.UnitTests.csproj
├── BufferedStreamWriterTests.cs
├── FromBase64TransformTests.cs
├── MemoryStreamExtensionsTests.cs
├── StringExtensionsTests.cs
└── TransformBase64StreamTests.cs
├── Base64.sln
├── Base64
├── Base64.csproj
├── Base64Buffer.cs
├── Block.cs
├── BufferedStreamWriter.cs
├── EncodingBuffer.cs
├── FromBase64Transform.cs
├── Infra
│ ├── MemoryStreamFactory.cs
│ └── ObjectPool.cs
├── MemoryStreamExtensions.cs
├── PooledBase64Buffer.cs
├── PooledUtf8EncodingBuffer.cs
├── StringExtensions.cs
├── TransformBase64Stream.cs
└── UnsafeConvert.cs
├── Benchmark
├── Benchmark.csproj
├── Program.cs
├── Stream
│ ├── StreamFromBase64.cs
│ └── StreamToBase64.cs
└── String
│ ├── StringFromBase64.cs
│ ├── StringFromBase64Loh.cs
│ └── StringToBase64.cs
├── LICENSE
├── Profiling
├── Profiling.csproj
└── Program.cs
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # TeamCity is a build add-in
131 | _TeamCity*
132 |
133 | # DotCover is a Code Coverage Tool
134 | *.dotCover
135 |
136 | # AxoCover is a Code Coverage Tool
137 | .axoCover/*
138 | !.axoCover/settings.json
139 |
140 | # Visual Studio code coverage results
141 | *.coverage
142 | *.coveragexml
143 |
144 | # NCrunch
145 | _NCrunch_*
146 | .*crunch*.local.xml
147 | nCrunchTemp_*
148 |
149 | # MightyMoose
150 | *.mm.*
151 | AutoTest.Net/
152 |
153 | # Web workbench (sass)
154 | .sass-cache/
155 |
156 | # Installshield output folder
157 | [Ee]xpress/
158 |
159 | # DocProject is a documentation generator add-in
160 | DocProject/buildhelp/
161 | DocProject/Help/*.HxT
162 | DocProject/Help/*.HxC
163 | DocProject/Help/*.hhc
164 | DocProject/Help/*.hhk
165 | DocProject/Help/*.hhp
166 | DocProject/Help/Html2
167 | DocProject/Help/html
168 |
169 | # Click-Once directory
170 | publish/
171 |
172 | # Publish Web Output
173 | *.[Pp]ublish.xml
174 | *.azurePubxml
175 | # Note: Comment the next line if you want to checkin your web deploy settings,
176 | # but database connection strings (with potential passwords) will be unencrypted
177 | *.pubxml
178 | *.publishproj
179 |
180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
181 | # checkin your Azure Web App publish settings, but sensitive information contained
182 | # in these scripts will be unencrypted
183 | PublishScripts/
184 |
185 | # NuGet Packages
186 | *.nupkg
187 | # NuGet Symbol Packages
188 | *.snupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 | *.appxbundle
214 | *.appxupload
215 |
216 | # Visual Studio cache files
217 | # files ending in .cache can be ignored
218 | *.[Cc]ache
219 | # but keep track of directories ending in .cache
220 | !?*.[Cc]ache/
221 |
222 | # Others
223 | ClientBin/
224 | ~$*
225 | *~
226 | *.dbmdl
227 | *.dbproj.schemaview
228 | *.jfm
229 | *.pfx
230 | *.publishsettings
231 | orleans.codegen.cs
232 |
233 | # Including strong name files can present a security risk
234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
235 | #*.snk
236 |
237 | # Since there are multiple workflows, uncomment next line to ignore bower_components
238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
239 | #bower_components/
240 |
241 | # RIA/Silverlight projects
242 | Generated_Code/
243 |
244 | # Backup & report files from converting an old project file
245 | # to a newer Visual Studio version. Backup files are not needed,
246 | # because we have git ;-)
247 | _UpgradeReport_Files/
248 | Backup*/
249 | UpgradeLog*.XML
250 | UpgradeLog*.htm
251 | ServiceFabricBackup/
252 | *.rptproj.bak
253 |
254 | # SQL Server files
255 | *.mdf
256 | *.ldf
257 | *.ndf
258 |
259 | # Business Intelligence projects
260 | *.rdl.data
261 | *.bim.layout
262 | *.bim_*.settings
263 | *.rptproj.rsuser
264 | *- [Bb]ackup.rdl
265 | *- [Bb]ackup ([0-9]).rdl
266 | *- [Bb]ackup ([0-9][0-9]).rdl
267 |
268 | # Microsoft Fakes
269 | FakesAssemblies/
270 |
271 | # GhostDoc plugin setting file
272 | *.GhostDoc.xml
273 |
274 | # Node.js Tools for Visual Studio
275 | .ntvs_analysis.dat
276 | node_modules/
277 |
278 | # Visual Studio 6 build log
279 | *.plg
280 |
281 | # Visual Studio 6 workspace options file
282 | *.opt
283 |
284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285 | *.vbw
286 |
287 | # Visual Studio LightSwitch build output
288 | **/*.HTMLClient/GeneratedArtifacts
289 | **/*.DesktopClient/GeneratedArtifacts
290 | **/*.DesktopClient/ModelManifest.xml
291 | **/*.Server/GeneratedArtifacts
292 | **/*.Server/ModelManifest.xml
293 | _Pvt_Extensions
294 |
295 | # Paket dependency manager
296 | .paket/paket.exe
297 | paket-files/
298 |
299 | # FAKE - F# Make
300 | .fake/
301 |
302 | # CodeRush personal settings
303 | .cr/personal
304 |
305 | # Python Tools for Visual Studio (PTVS)
306 | __pycache__/
307 | *.pyc
308 |
309 | # Cake - Uncomment if you are using it
310 | # tools/**
311 | # !tools/packages.config
312 |
313 | # Tabs Studio
314 | *.tss
315 |
316 | # Telerik's JustMock configuration file
317 | *.jmconfig
318 |
319 | # BizTalk build output
320 | *.btp.cs
321 | *.btm.cs
322 | *.odx.cs
323 | *.xsd.cs
324 |
325 | # OpenCover UI analysis results
326 | OpenCover/
327 |
328 | # Azure Stream Analytics local run output
329 | ASALocalRun/
330 |
331 | # MSBuild Binary and Structured Log
332 | *.binlog
333 |
334 | # NVidia Nsight GPU debugger configuration file
335 | *.nvuser
336 |
337 | # MFractors (Xamarin productivity tool) working folder
338 | .mfractor/
339 |
340 | # Local History for Visual Studio
341 | .localhistory/
342 |
343 | # BeatPulse healthcheck temp database
344 | healthchecksdb
345 |
346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
347 | MigrationBackup/
348 |
349 | # Ionide (cross platform F# VS Code tools) working folder
350 | .ionide/
351 |
--------------------------------------------------------------------------------
/Base64.UnitTests/Base64.UnitTests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net472;net48;net5.0;
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Base64.UnitTests/BufferedStreamWriterTests.cs:
--------------------------------------------------------------------------------
1 | using FluentAssertions;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading;
9 | using System.Threading.Tasks;
10 |
11 | namespace Base64.UnitTests
12 | {
13 | [TestClass]
14 | public class BufferedStreamWriterTests
15 | {
16 |
17 | protected virtual Stream CreateStream()
18 | {
19 | return new MemoryStream();
20 | }
21 |
22 | private EncodingBuffer GetUtf8Buffer(int size = 1024)
23 | {
24 | return new EncodingBuffer(NoBomEncoding.UTF8, size);
25 | }
26 |
27 | private EncodingBuffer GetAsciiBuffer(int size = 1024)
28 | {
29 | return new EncodingBuffer(NoBomEncoding.ASCII, size);
30 | }
31 |
32 | [TestMethod]
33 | public void WriteChars()
34 | {
35 | char[] chArr = TestDataProvider.CharData;
36 |
37 | // [] Write a wide variety of characters and read them back
38 |
39 | Stream ms = CreateStream();
40 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
41 | StreamReader sr;
42 |
43 | for (int i = 0; i < chArr.Length; i++)
44 | sw.Write(chArr[i]);
45 |
46 | sw.Flush();
47 | ms.Position = 0;
48 | sr = new StreamReader(ms);
49 |
50 | for (int i = 0; i < chArr.Length; i++)
51 | {
52 | sr.Read().Should().Be((int)chArr[i]);
53 | }
54 | }
55 |
56 | [TestMethod]
57 | public void NullArray()
58 | {
59 | // [] Exception for null array
60 | Stream ms = CreateStream();
61 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
62 |
63 | sw.Invoking(s => s.Write(null, 0, 0)).Should().Throw();
64 | sw.Dispose();
65 | }
66 |
67 | [TestMethod]
68 | public void NegativeOffset()
69 | {
70 | char[] chArr = TestDataProvider.CharData;
71 |
72 | // [] Exception if offset is negative
73 | Stream ms = CreateStream();
74 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
75 |
76 | sw.Invoking(s => s.Write(chArr, -1, 0)).Should().Throw();
77 | sw.Dispose();
78 | }
79 |
80 | [TestMethod]
81 | public void NegativeCount()
82 | {
83 | char[] chArr = TestDataProvider.CharData;
84 |
85 | // [] Exception if count is negative
86 | Stream ms = CreateStream();
87 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
88 |
89 | sw.Invoking(s => s.Write(chArr, 0, -1)).Should().Throw();
90 | sw.Dispose();
91 | }
92 |
93 | [TestMethod]
94 | public void WriteCustomLenghtStrings()
95 | {
96 | char[] chArr = TestDataProvider.CharData;
97 |
98 | // [] Write some custom length strings
99 | Stream ms = CreateStream();
100 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
101 | StreamReader sr;
102 |
103 | sw.Write(chArr, 2, 5);
104 | sw.Flush();
105 | ms.Position = 0;
106 | sr = new StreamReader(ms);
107 |
108 | for (int i = 2; i < 7; i++)
109 | {
110 | sr.Read().Should().Be((int)chArr[i]);
111 | }
112 | ms.Dispose();
113 | }
114 |
115 | [TestMethod]
116 | public void WriteToStreamWriter()
117 | {
118 | char[] chArr = TestDataProvider.CharData;
119 | // [] Just construct a streamwriter and write to it
120 | //-------------------------------------------------
121 | Stream ms = CreateStream();
122 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
123 | StreamReader sr;
124 |
125 | sw.Write(chArr, 0, chArr.Length);
126 | sw.Flush();
127 | ms.Position = 0;
128 | sr = new StreamReader(ms);
129 |
130 | for (int i = 0; i < chArr.Length; i++)
131 | {
132 | sr.Read().Should().Be((int)chArr[i]);
133 | }
134 | ms.Dispose();
135 | }
136 |
137 | [TestMethod]
138 | public void TestWritingPastEndOfArray()
139 | {
140 | char[] chArr = TestDataProvider.CharData;
141 | Stream ms = CreateStream();
142 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
143 |
144 | sw.Invoking(s => s.Write(chArr, 1, chArr.Length)).Should().Throw();
145 | sw.Dispose();
146 | }
147 |
148 | [TestMethod]
149 | public void VerifyWrittenString()
150 | {
151 | char[] chArr = TestDataProvider.CharData;
152 | // [] Write string with wide selection of characters and read it back
153 |
154 | StringBuilder sb = new StringBuilder(40);
155 | Stream ms = CreateStream();
156 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
157 | StreamReader sr;
158 |
159 | for (int i = 0; i < chArr.Length; i++)
160 | sb.Append(chArr[i]);
161 |
162 | sw.Write(sb.ToString());
163 | sw.Flush();
164 | ms.Position = 0;
165 | sr = new StreamReader(ms);
166 |
167 | for (int i = 0; i < chArr.Length; i++)
168 | {
169 | sr.Read().Should().Be((int)chArr[i]);
170 | }
171 | }
172 |
173 | [TestMethod]
174 | public void NullStringWritesNothing()
175 | {
176 | // [] Null string should write nothing
177 |
178 | Stream ms = CreateStream();
179 | var sw = new BufferedStreamWriter(ms, GetUtf8Buffer());
180 | sw.Write((string)null);
181 | sw.Flush();
182 | ms.Length.Should().Be(0);
183 | }
184 |
185 | [TestMethod]
186 | public void Write_CharArray_WritesExpectedData()
187 | {
188 | Write_CharArray_WritesExpectedData(1, 1, 1, false);
189 | Write_CharArray_WritesExpectedData(100, 1, 100, false);
190 | Write_CharArray_WritesExpectedData(100, 10, 3, false);
191 | Write_CharArray_WritesExpectedData(1, 1, 1, true);
192 | Write_CharArray_WritesExpectedData(100, 1, 100, true);
193 | Write_CharArray_WritesExpectedData(100, 10, 3, true);
194 | }
195 |
196 | public void Write_CharArray_WritesExpectedData(int length, int writeSize, int writerBufferSize, bool autoFlush)
197 | {
198 | using (var s = new MemoryStream())
199 | using (var writer = new BufferedStreamWriter(s, GetAsciiBuffer(writerBufferSize), autoFlush))
200 | {
201 | var data = new char[length];
202 | var rand = new Random(42);
203 | for (int i = 0; i < data.Length; i++)
204 | {
205 | data[i] = (char)(rand.Next(0, 26) + 'a');
206 | }
207 |
208 | int index = 0;
209 |
210 | while (index < data.Length)
211 | {
212 | int n = Math.Min(data.Length, writeSize);
213 | writer.Write(data, index, n);
214 | index += n;
215 | }
216 |
217 | writer.Flush();
218 |
219 | s.ToArray().Select(b => (char)b).Should().BeEquivalentTo(data);
220 | }
221 | }
222 |
223 | [TestMethod]
224 | public void WriteLine_CharArray_WritesExpectedData()
225 | {
226 | WriteLine_CharArray_WritesExpectedData(1, 1, 1, false);
227 | WriteLine_CharArray_WritesExpectedData(100, 1, 100, false);
228 | WriteLine_CharArray_WritesExpectedData(100, 10, 3, false);
229 | WriteLine_CharArray_WritesExpectedData(1, 1, 1, true);
230 | WriteLine_CharArray_WritesExpectedData(100, 1, 100, true);
231 | WriteLine_CharArray_WritesExpectedData(100, 10, 3, true);
232 | }
233 |
234 | public void WriteLine_CharArray_WritesExpectedData(int length, int writeSize, int writerBufferSize, bool autoFlush)
235 | {
236 | using (var s = new MemoryStream())
237 | using (var writer = new StreamWriter(s, Encoding.ASCII, writerBufferSize) { AutoFlush = autoFlush })
238 | {
239 | var data = new char[length];
240 | var rand = new Random(42);
241 | for (int i = 0; i < data.Length; i++)
242 | {
243 | data[i] = (char)(rand.Next(0, 26) + 'a');
244 | }
245 |
246 | int index = 0;
247 |
248 | while (index < data.Length)
249 | {
250 | int n = Math.Min(data.Length, writeSize);
251 | writer.WriteLine(data, index, n);
252 | index += n;
253 | }
254 |
255 | writer.Flush();
256 |
257 | s.Length.Should().Be(length + (Environment.NewLine.Length * (length / writeSize)));
258 | }
259 | }
260 |
261 | [TestMethod]
262 | public void AfterDisposeThrows()
263 | {
264 | // [] Calling methods after disposing the stream should throw
265 | //-----------------------------------------------------------------
266 | var sw2 = new BufferedStreamWriter(new MemoryStream(), GetUtf8Buffer());
267 | sw2.Dispose();
268 | ValidateDisposedExceptions(sw2);
269 | }
270 |
271 | [TestMethod]
272 | public void AfterCloseThrows()
273 | {
274 | // [] Calling methods after closing the stream should throw
275 | //-----------------------------------------------------------------
276 | var sw2 = new BufferedStreamWriter(new MemoryStream(), GetUtf8Buffer());
277 | sw2.Close();
278 | ValidateDisposedExceptions(sw2);
279 | }
280 |
281 | private void ValidateDisposedExceptions(BufferedStreamWriter sw)
282 | {
283 | sw.Invoking(w => w.Write('A')).Should().Throw();
284 | sw.Invoking(w => w.Write("hello")).Should().Throw();
285 | sw.Invoking(w => w.Flush()).Should().Throw();
286 | }
287 |
288 | [TestMethod]
289 | public void CloseCausesFlush()
290 | {
291 | Stream memstr2;
292 |
293 | // [] Check that flush updates the underlying stream
294 | //-----------------------------------------------------------------
295 | memstr2 = CreateStream();
296 | var sw2 = new BufferedStreamWriter(memstr2, GetUtf8Buffer());
297 |
298 | var strTemp = "HelloWorld";
299 | sw2.Write(strTemp);
300 |
301 | memstr2.Length.Should().Be(0);
302 |
303 | sw2.Flush();
304 | memstr2.Length.Should().Be(strTemp.Length);
305 |
306 | }
307 |
308 | [TestMethod]
309 | public void CantFlushAfterDispose()
310 | {
311 | // [] Flushing disposed writer should throw
312 | //-----------------------------------------------------------------
313 |
314 | Stream memstr2 = CreateStream();
315 | var sw2 = new BufferedStreamWriter(memstr2, GetUtf8Buffer());
316 |
317 | sw2.Dispose();
318 | sw2.Invoking(w => w.Flush()).Should().Throw();
319 | }
320 |
321 | [TestMethod]
322 | public void CantFlushAfterClose()
323 | {
324 | // [] Flushing closed writer should throw
325 | //-----------------------------------------------------------------
326 |
327 | Stream memstr2 = CreateStream();
328 | var sw2 = new BufferedStreamWriter(memstr2, GetUtf8Buffer());
329 |
330 | sw2.Close();
331 | sw2.Invoking(w => w.Flush()).Should().Throw();
332 | }
333 | }
334 |
335 | public static class TestDataProvider
336 | {
337 | private static readonly char[] s_charData;
338 | private static readonly char[] s_smallData;
339 | private static readonly char[] s_largeData;
340 |
341 | public static object FirstObject { get; } = (object)1;
342 | public static object SecondObject { get; } = (object)"[second object]";
343 | public static object ThirdObject { get; } = (object)"";
344 | public static object[] MultipleObjects { get; } = new object[] { FirstObject, SecondObject, ThirdObject };
345 |
346 | public static string FormatStringOneObject { get; } = "Object is {0}";
347 | public static string FormatStringTwoObjects { get; } = $"Object are '{0}', {SecondObject}";
348 | public static string FormatStringThreeObjects { get; } = $"Objects are {0}, {SecondObject}, {ThirdObject}";
349 | public static string FormatStringMultipleObjects { get; } = "Multiple Objects are: {0}, {1}, {2}";
350 |
351 | static TestDataProvider()
352 | {
353 | s_charData = new char[]
354 | {
355 | char.MinValue,
356 | char.MaxValue,
357 | '\t',
358 | ' ',
359 | '$',
360 | '@',
361 | '#',
362 | '\0',
363 | '\v',
364 | '\'',
365 | '\u3190',
366 | '\uC3A0',
367 | 'A',
368 | '5',
369 | '\r',
370 | '\uFE70',
371 | '-',
372 | ';',
373 | '\r',
374 | '\n',
375 | 'T',
376 | '3',
377 | '\n',
378 | 'K',
379 | '\u00E6'
380 | };
381 |
382 | s_smallData = "HELLO".ToCharArray();
383 |
384 | var data = new List();
385 | for (int count = 0; count < 1000; ++count)
386 | {
387 | data.AddRange(s_smallData);
388 | }
389 | s_largeData = data.ToArray();
390 | }
391 |
392 | public static char[] CharData => s_charData;
393 |
394 | public static char[] SmallData => s_smallData;
395 |
396 | public static char[] LargeData => s_largeData;
397 | }
398 |
399 | internal sealed class DelegateStream : Stream
400 | {
401 | private readonly Func _canReadFunc;
402 | private readonly Func _canSeekFunc;
403 | private readonly Func _canWriteFunc;
404 | private readonly Action _flushFunc = null;
405 | private readonly Func _flushAsyncFunc = null;
406 | private readonly Func _lengthFunc;
407 | private readonly Func _positionGetFunc;
408 | private readonly Action _positionSetFunc;
409 | private readonly Func _readFunc;
410 | private readonly Func> _readAsyncFunc;
411 | private readonly Func _seekFunc;
412 | private readonly Action _setLengthFunc;
413 | private readonly Action _writeFunc;
414 | private readonly Func _writeAsyncFunc;
415 | private readonly Action _disposeFunc;
416 |
417 | public DelegateStream(
418 | Func canReadFunc = null,
419 | Func canSeekFunc = null,
420 | Func canWriteFunc = null,
421 | Action flushFunc = null,
422 | Func flushAsyncFunc = null,
423 | Func lengthFunc = null,
424 | Func positionGetFunc = null,
425 | Action positionSetFunc = null,
426 | Func readFunc = null,
427 | Func> readAsyncFunc = null,
428 | Func seekFunc = null,
429 | Action setLengthFunc = null,
430 | Action writeFunc = null,
431 | Func writeAsyncFunc = null,
432 | Action disposeFunc = null)
433 | {
434 | _canReadFunc = canReadFunc ?? (() => false);
435 | _canSeekFunc = canSeekFunc ?? (() => false);
436 | _canWriteFunc = canWriteFunc ?? (() => false);
437 |
438 | _flushFunc = flushFunc ?? (() => { });
439 | _flushAsyncFunc = flushAsyncFunc ?? (token => base.FlushAsync(token));
440 |
441 | _lengthFunc = lengthFunc ?? (() => { throw new NotSupportedException(); });
442 | _positionSetFunc = positionSetFunc ?? (_ => { throw new NotSupportedException(); });
443 | _positionGetFunc = positionGetFunc ?? (() => { throw new NotSupportedException(); });
444 |
445 | _readAsyncFunc = readAsyncFunc ?? ((buffer, offset, count, token) => base.ReadAsync(buffer, offset, count, token));
446 | _readFunc = readFunc ?? ((buffer, offset, count) => readAsyncFunc(buffer, offset, count, default).GetAwaiter().GetResult());
447 |
448 | _seekFunc = seekFunc ?? ((_, __) => { throw new NotSupportedException(); });
449 | _setLengthFunc = setLengthFunc ?? (_ => { throw new NotSupportedException(); });
450 |
451 | _writeAsyncFunc = writeAsyncFunc ?? ((buffer, offset, count, token) => base.WriteAsync(buffer, offset, count, token));
452 | _writeFunc = writeFunc ?? ((buffer, offset, count) => writeAsyncFunc(buffer, offset, count, default).GetAwaiter().GetResult());
453 |
454 | _disposeFunc = disposeFunc;
455 | }
456 |
457 | public override bool CanRead { get { return _canReadFunc(); } }
458 | public override bool CanSeek { get { return _canSeekFunc(); } }
459 | public override bool CanWrite { get { return _canWriteFunc(); } }
460 |
461 | public override void Flush() { _flushFunc(); }
462 | public override Task FlushAsync(CancellationToken cancellationToken) { return _flushAsyncFunc(cancellationToken); }
463 |
464 | public override long Length { get { return _lengthFunc(); } }
465 | public override long Position { get { return _positionGetFunc(); } set { _positionSetFunc(value); } }
466 |
467 | public override int Read(byte[] buffer, int offset, int count) { return _readFunc(buffer, offset, count); }
468 | public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return _readAsyncFunc(buffer, offset, count, cancellationToken); }
469 |
470 | public override long Seek(long offset, SeekOrigin origin) { return _seekFunc(offset, origin); }
471 | public override void SetLength(long value) { _setLengthFunc(value); }
472 |
473 | public override void Write(byte[] buffer, int offset, int count) { _writeFunc(buffer, offset, count); }
474 | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return _writeAsyncFunc(buffer, offset, count, cancellationToken); }
475 |
476 | protected override void Dispose(bool disposing) { _disposeFunc?.Invoke(disposing); }
477 | }
478 | }
479 |
--------------------------------------------------------------------------------
/Base64.UnitTests/FromBase64TransformTests.cs:
--------------------------------------------------------------------------------
1 | using FluentAssertions;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Runtime.InteropServices;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Base64.UnitTests
11 | {
12 | [TestClass]
13 | public class FromBase64TransformTests
14 | {
15 | private readonly FromBase64Transform t = new FromBase64Transform();
16 |
17 | private readonly Block block = new Block(0, 4096);
18 |
19 | // Tests RFC 4648 section 10 test vectors.
20 | // @see http://tools.ietf.org/
21 | [TestMethod]
22 | public void RFC4648()
23 | {
24 | Validate("");
25 | Validate("Zg==");
26 | Validate("Zm8=");
27 | Validate("Zm9v");
28 | Validate("Zm9vYg==");
29 | Validate("Zm9vYmE=");
30 | Validate("Zm9vYmFy");
31 | }
32 |
33 | [TestMethod]
34 | public void InvalidFormatFirstBlock()
35 | {
36 | InvalidFormatFirstBlock("====");
37 | InvalidFormatFirstBlock("aaa!");
38 | InvalidFormatFirstBlock("=aaa");
39 | InvalidFormatFirstBlock("a=aa");
40 | }
41 |
42 | [TestMethod]
43 | public void InvalidFormatFinalBlock()
44 | {
45 | InvalidFormatFinalBlock("=", "");
46 | InvalidFormatFinalBlock("==", "");
47 | InvalidFormatFinalBlock("===", "");
48 |
49 | InvalidFormatFinalBlock("a", "");
50 | InvalidFormatFinalBlock("!", "");
51 | InvalidFormatFinalBlock("aa!", "");
52 | }
53 |
54 | [TestMethod]
55 | public void InvalidLength()
56 | {
57 | InvalidFormatFinalBlock("aaaaa", "");
58 | InvalidFormatFinalBlock("aaaa", "a");
59 | InvalidFormatFinalBlock("aaa", "aa");
60 | }
61 |
62 | [TestMethod]
63 | public void LeadingSpace()
64 | {
65 | Validate(" YWFhYQ==");
66 | }
67 |
68 | [TestMethod]
69 | public void TrailingSpace()
70 | {
71 | Validate("YWFhYQ== ");
72 | }
73 |
74 | [TestMethod]
75 | public void SingleBlockInnerSpace()
76 | {
77 | Validate("YWFh YQ==");
78 | Validate("YW FhYQ==");
79 | Validate("YWFhYQ ==");
80 | }
81 |
82 | [TestMethod]
83 | public void SingleBlockWithMultipleSpace()
84 | {
85 | Validate(" a a a a ");
86 | }
87 |
88 | [TestMethod]
89 | public void SingleBlockInnerSpaceLong()
90 | {
91 | string base64string = new string('a', 2016) + "Zm 9v";
92 | Validate(base64string);
93 | }
94 |
95 | [TestMethod]
96 | public void TwoBlocksContinuous()
97 | {
98 | ValidateMultiblock("aa", "aa");
99 | }
100 |
101 | [TestMethod]
102 | public void TwoBlocksWithSpace()
103 | {
104 | ValidateMultiblock(" a a ", " a a ");
105 | ValidateMultiblock(" a a", " a a ");
106 | ValidateMultiblock(" a a ", " a a ");
107 | ValidateMultiblock(" a a ", "a a ");
108 | }
109 |
110 | [TestMethod]
111 | public void ThreeBlocksContinuous()
112 | {
113 | ValidateMultiblock("aaaa", "aaa", "a");
114 | }
115 |
116 | [TestMethod]
117 | public void ThreeBlocksWithSpace()
118 | {
119 | ValidateMultiblock(" a a a ", " a a a ", " a a");
120 | ValidateMultiblock(" a a a", " a a a ", " a a");
121 | ValidateMultiblock(" a a a ", "a a a ", " a a");
122 | ValidateMultiblock(" a a a ", " a a a", " a a");
123 | ValidateMultiblock(" a a a ", " a a a ", "a a");
124 | ValidateMultiblock(" a a a", " a a a", " a a");
125 | ValidateMultiblock(" a a a", " a a a ", "a a");
126 | ValidateMultiblock(" a a a ", "a a a", " a a");
127 | ValidateMultiblock(" a a a ", "a a a ", "a a");
128 | }
129 |
130 | private void Validate(string input)
131 | {
132 | var reference = Convert.FromBase64String(input);
133 |
134 | var bytes = Encoding.ASCII.GetBytes(input);
135 |
136 | this.block.input = bytes;
137 | this.block.inputOffset = 0;
138 | this.block.inputCount = bytes.Length;
139 | this.block.outputOffset = 0;
140 |
141 | int len = t.TransformBlock(block);
142 |
143 | len.Should().Be(reference.Length);
144 |
145 | for (int i = 0; i < reference.Length; i++)
146 | {
147 | block.outputBuffer[i].Should().Be(reference[i], $"Difference at {i}");
148 | }
149 | }
150 |
151 | private void InvalidFormatFirstBlock(string input)
152 | {
153 | this.block.Reset();
154 | t.Reset();
155 |
156 | var bytes = Encoding.ASCII.GetBytes(input);
157 |
158 | this.block.input = bytes;
159 | this.block.inputOffset = 0;
160 | this.block.inputCount = bytes.Length;
161 | this.block.outputOffset = 0;
162 |
163 | t.Invoking(x => x.TransformBlock(block)).Should().Throw();
164 | }
165 |
166 | private void InvalidFormatFinalBlock(string first, string final)
167 | {
168 | this.block.Reset();
169 | t.Reset();
170 |
171 | var bytes = Encoding.ASCII.GetBytes(first);
172 |
173 | this.block.input = bytes;
174 | this.block.inputOffset = 0;
175 | this.block.inputCount = bytes.Length;
176 | this.block.outputOffset = 0;
177 |
178 | this.block.outputOffset += t.TransformBlock(block);
179 |
180 | bytes = Encoding.ASCII.GetBytes(final);
181 |
182 | this.block.input = bytes;
183 | this.block.inputOffset = 0;
184 | this.block.inputCount = bytes.Length;
185 |
186 | t.Invoking(x => x.TransformFinalBlock(block)).Should().Throw();
187 | }
188 |
189 | private void ValidateMultiblock(string block1, string block2)
190 | {
191 | var reference = Convert.FromBase64String(block1 + block2);
192 |
193 | var bytes1 = Encoding.ASCII.GetBytes(block1);
194 |
195 | this.block.input = bytes1;
196 | this.block.inputOffset = 0;
197 | this.block.inputCount = bytes1.Length;
198 | this.block.outputOffset = 0;
199 |
200 | int bytesWritten = t.TransformBlock(block);
201 |
202 | var bytes2 = Encoding.ASCII.GetBytes(block2);
203 |
204 | this.block.input = bytes2;
205 | this.block.inputOffset = 0;
206 | this.block.inputCount = bytes2.Length;
207 | this.block.outputOffset = bytesWritten;
208 |
209 | bytesWritten += t.TransformFinalBlock(block);
210 |
211 | for (int i = 0; i < reference.Length; i++)
212 | {
213 | block.outputBuffer[i].Should().Be(reference[i], $"Difference at {i}");
214 | }
215 |
216 | bytesWritten.Should().Be(reference.Length);
217 | }
218 |
219 | private void ValidateMultiblock(string block1, string block2, string block3)
220 | {
221 | var reference = Convert.FromBase64String(block1 + block2 + block3);
222 |
223 | var bytes1 = Encoding.ASCII.GetBytes(block1);
224 |
225 | this.block.input = bytes1;
226 | this.block.inputOffset = 0;
227 | this.block.inputCount = bytes1.Length;
228 | this.block.outputOffset = 0;
229 |
230 | int bytesWritten = t.TransformBlock(block);
231 |
232 | var bytes2 = Encoding.ASCII.GetBytes(block2);
233 |
234 | this.block.input = bytes2;
235 | this.block.inputOffset = 0;
236 | this.block.inputCount = bytes2.Length;
237 | this.block.outputOffset = bytesWritten;
238 |
239 | bytesWritten += t.TransformBlock(block);
240 |
241 | var bytes3 = Encoding.ASCII.GetBytes(block3);
242 |
243 | this.block.input = bytes3;
244 | this.block.inputOffset = 0;
245 | this.block.inputCount = bytes3.Length;
246 | this.block.outputOffset = bytesWritten;
247 |
248 | bytesWritten += t.TransformFinalBlock(block);
249 |
250 | for (int i = 0; i < reference.Length; i++)
251 | {
252 | block.outputBuffer[i].Should().Be(reference[i], $"Difference at {i}");
253 | }
254 |
255 | bytesWritten.Should().Be(reference.Length);
256 | }
257 | }
258 | }
259 |
--------------------------------------------------------------------------------
/Base64.UnitTests/MemoryStreamExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Text;
4 | using FluentAssertions;
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
6 |
7 | namespace Base64.UnitTests
8 | {
9 | [TestClass]
10 | public class MemoryStreamExtensionsTests
11 | {
12 | [TestMethod]
13 | public void WriteUtf8AndSetStartMatchesUtf8EncodingGetBytes()
14 | {
15 | string test = "foo";
16 | var referenceBytes = Encoding.UTF8.GetBytes(test);
17 |
18 | using (var s = new MemoryStream())
19 | {
20 | s.WriteUtf8AndSetStart(test);
21 |
22 | var writtenBytes = s.ToArray();
23 |
24 | writtenBytes.Should().BeEquivalentTo(referenceBytes);
25 | }
26 | }
27 |
28 | [TestMethod]
29 | public void ReadToBase64MatchesConvertToBase64String()
30 | {
31 | string test = "foo";
32 |
33 | // we are trying to replace this code, so verify we produce the same result:
34 | var rawBytes = Encoding.UTF8.GetBytes(test);
35 | var convertString = Convert.ToBase64String(rawBytes);
36 |
37 | using (var s = new MemoryStream())
38 | {
39 | s.WriteUtf8AndSetStart(test);
40 |
41 | var streamString = s.ReadToBase64();
42 |
43 | streamString.Should().Be(convertString);
44 | }
45 | }
46 |
47 | [TestMethod]
48 | public void WriteFromBase64MatchesConvertFromBase64()
49 | {
50 | string input = "foo";
51 | var utf8Base64String = input.ToUtf8Base64String();
52 |
53 | using (var s = new MemoryStream())
54 | {
55 | s.WriteFromBase64(utf8Base64String);
56 |
57 | s.Position = 0;
58 |
59 | var streamBase64Bytes = s.ToArray();
60 | var convertBase64Bytes = Convert.FromBase64String(utf8Base64String);
61 |
62 | streamBase64Bytes.Should().BeEquivalentTo(convertBase64Bytes);
63 |
64 | // also check we can actually decode the bytes back to the original string
65 | s.TryGetBuffer(out var buffer);
66 | var decoded = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);
67 |
68 | decoded.Should().Be(input);
69 | }
70 | }
71 |
72 | // if we fail and the internal structures are not reset before subsequent calls, we can be in
73 | // a corrupt state (e.g. temp buffers contain partial data). This test is to check that everything
74 | // is correctly reset
75 | [TestMethod]
76 | public void WriteFromBase64FailThenPass()
77 | {
78 | string input = "foo";
79 | var utf8Base64String = input.ToUtf8Base64String();
80 |
81 | using (var stream = new MemoryStream())
82 | {
83 | stream.Invoking(s => s.WriteFromBase64("aaaaa")).Should().Throw();
84 | }
85 |
86 | using (var s = new MemoryStream())
87 | {
88 | s.WriteFromBase64(utf8Base64String);
89 |
90 | s.Position = 0;
91 |
92 | var streamBase64Bytes = s.ToArray();
93 | var convertBase64Bytes = Convert.FromBase64String(utf8Base64String);
94 |
95 | streamBase64Bytes.Should().BeEquivalentTo(convertBase64Bytes);
96 | }
97 | }
98 |
99 | [TestMethod]
100 | public void RFC4648ExamplesFromBase64ProduceExpectedOutput()
101 | {
102 | VerifyWriteFromBase64("", "");
103 | VerifyWriteFromBase64("Zg==", "f");
104 | VerifyWriteFromBase64("Zm8=", "fo");
105 | VerifyWriteFromBase64("Zm9v", "foo");
106 | VerifyWriteFromBase64("Zm9vYg==", "foob");
107 | VerifyWriteFromBase64("Zm9vYmE=", "fooba");
108 | VerifyWriteFromBase64("Zm9vYmFy", "foobar");
109 | }
110 |
111 | [TestMethod]
112 | public void FromBase64InvalidInputThrows()
113 | {
114 | // invalid length
115 | ValidateInvalidFrom64("a");
116 | ValidateInvalidFrom64("aa");
117 | ValidateInvalidFrom64("aaa");
118 | ValidateInvalidFrom64("aaaaa");
119 | ValidateInvalidFrom64("aaaaaa");
120 | ValidateInvalidFrom64("aaaaaaa");
121 |
122 | // invalid char
123 | ValidateInvalidFrom64("!aaa");
124 |
125 | // invalid padding
126 | ValidateInvalidFrom64("=aaa");
127 | ValidateInvalidFrom64("==aa");
128 | ValidateInvalidFrom64("a===");
129 | ValidateInvalidFrom64("====");
130 | }
131 |
132 | private static void VerifyWriteFromBase64(string base64, string expectedAscii)
133 | {
134 | using (var s = new MemoryStream())
135 | {
136 | s.WriteFromBase64(base64);
137 |
138 | s.Position = 0;
139 |
140 | var streamBase64Bytes = s.ToArray();
141 | var actual = Encoding.ASCII.GetString(streamBase64Bytes);
142 |
143 | actual.Should().Be(expectedAscii);
144 | }
145 | }
146 |
147 | private static void ValidateInvalidFrom64(string input)
148 | {
149 | using (var s = new MemoryStream())
150 | {
151 | s.Invoking(i => i.WriteFromBase64(input)).Should().Throw();
152 | }
153 | }
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/Base64.UnitTests/StringExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | using FluentAssertions;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Base64.UnitTests
10 | {
11 | [TestClass]
12 | public class StringExtensionsTests
13 | {
14 | [TestMethod]
15 | public void ToBase64()
16 | {
17 | string test = "foo";
18 |
19 | var referenceBytes = Encoding.UTF8.GetBytes(test);
20 | var convertString = Convert.ToBase64String(referenceBytes);
21 |
22 | var streamString = test.ToUtf8Base64String();
23 |
24 | streamString.Should().Be(convertString);
25 | }
26 |
27 | [TestMethod]
28 | public void ToBase64Long()
29 | {
30 | // short string cut off is < 160
31 | string test = new string('a', 160);
32 |
33 | var referenceBytes = Encoding.UTF8.GetBytes(test);
34 | var convertString = Convert.ToBase64String(referenceBytes);
35 |
36 | var streamString = test.ToUtf8Base64String();
37 |
38 | streamString.Should().Be(convertString);
39 | }
40 |
41 | [TestMethod]
42 | public void FromBase64()
43 | {
44 | string test = "foo";
45 | var base64string = test.ToUtf8Base64String();
46 |
47 | var resultBytes = Convert.FromBase64String(base64string);
48 | var convertString = Encoding.UTF8.GetString(resultBytes);
49 |
50 | var streamString = base64string.FromUtf8Base64String();
51 |
52 | streamString.Should().Be(convertString);
53 | }
54 |
55 | [TestMethod]
56 | public void FromBase64Long()
57 | {
58 | string test = new string('a', 2048);
59 | var base64string = test.ToUtf8Base64String();
60 |
61 | var resultBytes = Convert.FromBase64String(base64string);
62 | var convertString = Encoding.UTF8.GetString(resultBytes);
63 |
64 | var streamString = base64string.FromUtf8Base64String();
65 |
66 | streamString.Should().Be(convertString);
67 | }
68 |
69 | [TestMethod]
70 | public void FromBase64InternalSpace()
71 | {
72 | string base64string = new string('a', 2016) + "Zm 9v";
73 |
74 | var resultBytes = Convert.FromBase64String(base64string);
75 | var convertString = Encoding.UTF8.GetString(resultBytes);
76 |
77 | var streamString = base64string.FromUtf8Base64String();
78 |
79 | streamString.Should().Be(convertString);
80 | }
81 |
82 | [TestMethod]
83 | public void FromBase64TrailingSpace()
84 | {
85 | string base64string = new string('a', 2016) + "Zm9v ";
86 |
87 | var resultBytes = Convert.FromBase64String(base64string);
88 | var convertString = Encoding.UTF8.GetString(resultBytes);
89 |
90 | var streamString = base64string.FromUtf8Base64String();
91 |
92 | streamString.Should().Be(convertString);
93 | }
94 |
95 | [TestMethod]
96 | public void FromBase64LeadingSpace()
97 | {
98 | string base64string = " " + new string('a', 2016) + "Zm9v";
99 |
100 | var resultBytes = Convert.FromBase64String(base64string);
101 | var convertString = Encoding.UTF8.GetString(resultBytes);
102 |
103 | var streamString = base64string.FromUtf8Base64String();
104 |
105 | streamString.Should().Be(convertString);
106 | }
107 |
108 | [TestMethod]
109 | [ExpectedException(typeof(System.FormatException))]
110 | public void FromBase64WrongLength()
111 | {
112 | string base64string = new string('a', 2017);
113 |
114 | // The length of a base64 encoded string is always a multiple of 4
115 | var streamString = base64string.FromUtf8Base64String();
116 |
117 | Console.WriteLine(streamString);
118 | }
119 |
120 | [TestMethod]
121 | public void CompareFromUtf8Base64String()
122 | {
123 | const int size = 64 * 64;
124 | char[] alphabet = ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz".ToUpper() + "0123456789").ToCharArray();
125 |
126 | StringBuilder sb = new StringBuilder(size);
127 |
128 | for (int i = 0; i < size; i++)
129 | {
130 | sb.Append(alphabet[i % alphabet.Length]);
131 |
132 | var s2 = sb.ToString().ToUtf8Base64String();
133 |
134 | var resultBytes = Convert.FromBase64String(s2);
135 | var convertString = Encoding.UTF8.GetString(resultBytes);
136 |
137 | var xStr = s2.FromUtf8Base64String();
138 |
139 | xStr.Should().Be(convertString);
140 | }
141 | }
142 |
143 | // if we fail and the internal structures are not reset before subsequent calls, we can be in
144 | // a corrupt state (e.g. temp buffers contain partial data). This test is to check that everything
145 | // is correctly reset
146 | [TestMethod]
147 | public void FromBase64FailThenPass()
148 | {
149 | string test = "foo";
150 | var base64string = test.ToUtf8Base64String();
151 |
152 | var resultBytes = Convert.FromBase64String(base64string);
153 | var convertString = Encoding.UTF8.GetString(resultBytes);
154 |
155 | var invalidLength = "aaaaa";
156 |
157 | invalidLength.Invoking(i => i.FromUtf8Base64String()).Should().Throw();
158 |
159 | var streamString = base64string.FromUtf8Base64String();
160 |
161 | streamString.Should().Be(convertString);
162 | }
163 |
164 | [TestMethod]
165 | public void RFC4648ExamplesFromBase64ProduceExpectedOutput()
166 | {
167 | "".FromUtf8Base64String().Should().Be("");
168 | "Zg==".FromUtf8Base64String().Should().Be("f");
169 | "Zm8=".FromUtf8Base64String().Should().Be("fo");
170 | "Zm9v".FromUtf8Base64String().Should().Be("foo");
171 | "Zm9vYg==".FromUtf8Base64String().Should().Be("foob");
172 | "Zm9vYmE=".FromUtf8Base64String().Should().Be("fooba");
173 | "Zm9vYmFy".FromUtf8Base64String().Should().Be("foobar");
174 | }
175 |
176 | [TestMethod]
177 | public void FromBase64InvalidInputThrows()
178 | {
179 | // invalid length
180 | ValidateInvalidFrom64("a");
181 | ValidateInvalidFrom64("aa");
182 | ValidateInvalidFrom64("aaa");
183 | ValidateInvalidFrom64("aaaaa");
184 | ValidateInvalidFrom64("aaaaaa");
185 | ValidateInvalidFrom64("aaaaaaa");
186 |
187 | // invalid char
188 | ValidateInvalidFrom64("!aaa");
189 |
190 | // invalid padding
191 | ValidateInvalidFrom64("=aaa");
192 | ValidateInvalidFrom64("==aa");
193 | ValidateInvalidFrom64("a===");
194 | ValidateInvalidFrom64("====");
195 | }
196 |
197 | private static void ValidateInvalidFrom64(string input)
198 | {
199 | input.Invoking(i => i.FromUtf8Base64String()).Should().Throw();
200 | }
201 | }
202 | }
203 |
--------------------------------------------------------------------------------
/Base64.UnitTests/TransformBase64StreamTests.cs:
--------------------------------------------------------------------------------
1 | using FluentAssertions;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Base64.UnitTests
11 | {
12 | [TestClass]
13 | public class TransformBase64StreamTests
14 | {
15 | private readonly MemoryStream stream = new MemoryStream();
16 | private readonly EncodingBuffer encodingBuffer = new EncodingBuffer(NoBomEncoding.ASCII, 4096);
17 | private readonly Base64Buffer base64Buffer = new Base64Buffer();
18 |
19 | private int length = 4096 * 3;
20 |
21 | private string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/";
22 |
23 | [TestMethod]
24 | public void StringMultiChunkWhiteSpace1()
25 | {
26 | StringBuilder sb = new StringBuilder(length);
27 |
28 | for (int i = 0; i < length; i++)
29 | {
30 | sb.Append(" a");
31 | }
32 |
33 | ValidateString(sb.ToString());
34 | }
35 |
36 | [TestMethod]
37 | public void StringMultiChunkWhiteSpace2()
38 | {
39 | StringBuilder sb = new StringBuilder(length);
40 |
41 | for (int i = 0; i < length; i++)
42 | {
43 | sb.Append("a ");
44 | }
45 |
46 | ValidateString(sb.ToString());
47 | }
48 |
49 | [TestMethod]
50 | public void StringMultiChunkWhiteSpace3()
51 | {
52 | StringBuilder sb = new StringBuilder(length);
53 |
54 | for (int i = 0; i < length; i++)
55 | {
56 | sb.Append(" ");
57 | sb.Append(alphabet[i % alphabet.Length]);
58 | sb.Append(" ");
59 | }
60 |
61 | ValidateString(sb.ToString());
62 | }
63 |
64 | [TestMethod]
65 | public void StringMultiChunkWhiteSpace4()
66 | {
67 | StringBuilder sb = new StringBuilder(length);
68 |
69 | for (int i = 0; i < length; i++)
70 | {
71 | sb.Append(" aa");
72 | }
73 |
74 | ValidateString(sb.ToString());
75 | }
76 |
77 | [TestMethod]
78 | public void StringMultiChunkWhiteSpace5()
79 | {
80 | StringBuilder sb = new StringBuilder(length);
81 |
82 | for (int i = 0; i < length; i++)
83 | {
84 | sb.Append(" a");
85 | }
86 |
87 | ValidateString(sb.ToString());
88 | }
89 |
90 |
91 | private void ValidateString(string input)
92 | {
93 | var reference = Convert.FromBase64String(input);
94 |
95 | using (var base64Stream = new TransformBase64Stream(stream, base64Buffer, true))
96 | using (var w = new BufferedStreamWriter(base64Stream, encodingBuffer, true))
97 | {
98 | w.Write(input);
99 | }
100 |
101 | stream.TryGetBuffer(out var buffer);
102 |
103 | Console.WriteLine($"Exp: {Encoding.ASCII.GetString(reference)}");
104 | Console.WriteLine($"Act: {Encoding.ASCII.GetString(buffer.Array, buffer.Offset, buffer.Count)}");
105 |
106 | for (int i = 0; i < reference.Length; i++)
107 | {
108 | buffer.Array[i].Should().Be(reference[i], $"Difference at {i}");
109 | }
110 |
111 | buffer.Count.Should().Be(reference.Length);
112 | }
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/Base64.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30320.27
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Base64", "Base64\Base64.csproj", "{31BD7761-23B4-4ABF-9286-E8990D2D59E0}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Profiling", "Profiling\Profiling.csproj", "{19758639-369B-4C4B-B12F-59EA7217BC54}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmark", "Benchmark\Benchmark.csproj", "{9E7D1C42-BDC3-4A46-84EA-EFBDEF5FF873}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Base64.UnitTests", "Base64.UnitTests\Base64.UnitTests.csproj", "{C026C6AA-EC94-41DE-9A0A-B4E2091ADA68}"
13 | EndProject
14 | Global
15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
16 | Debug|Any CPU = Debug|Any CPU
17 | Release|Any CPU = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
20 | {31BD7761-23B4-4ABF-9286-E8990D2D59E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {31BD7761-23B4-4ABF-9286-E8990D2D59E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {31BD7761-23B4-4ABF-9286-E8990D2D59E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {31BD7761-23B4-4ABF-9286-E8990D2D59E0}.Release|Any CPU.Build.0 = Release|Any CPU
24 | {19758639-369B-4C4B-B12F-59EA7217BC54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25 | {19758639-369B-4C4B-B12F-59EA7217BC54}.Debug|Any CPU.Build.0 = Debug|Any CPU
26 | {19758639-369B-4C4B-B12F-59EA7217BC54}.Release|Any CPU.ActiveCfg = Release|Any CPU
27 | {19758639-369B-4C4B-B12F-59EA7217BC54}.Release|Any CPU.Build.0 = Release|Any CPU
28 | {9E7D1C42-BDC3-4A46-84EA-EFBDEF5FF873}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29 | {9E7D1C42-BDC3-4A46-84EA-EFBDEF5FF873}.Debug|Any CPU.Build.0 = Debug|Any CPU
30 | {9E7D1C42-BDC3-4A46-84EA-EFBDEF5FF873}.Release|Any CPU.ActiveCfg = Release|Any CPU
31 | {9E7D1C42-BDC3-4A46-84EA-EFBDEF5FF873}.Release|Any CPU.Build.0 = Release|Any CPU
32 | {C026C6AA-EC94-41DE-9A0A-B4E2091ADA68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33 | {C026C6AA-EC94-41DE-9A0A-B4E2091ADA68}.Debug|Any CPU.Build.0 = Debug|Any CPU
34 | {C026C6AA-EC94-41DE-9A0A-B4E2091ADA68}.Release|Any CPU.ActiveCfg = Release|Any CPU
35 | {C026C6AA-EC94-41DE-9A0A-B4E2091ADA68}.Release|Any CPU.Build.0 = Release|Any CPU
36 | EndGlobalSection
37 | GlobalSection(SolutionProperties) = preSolution
38 | HideSolutionNode = FALSE
39 | EndGlobalSection
40 | GlobalSection(ExtensibilityGlobals) = postSolution
41 | SolutionGuid = {734B7AD6-DF1F-4C50-988E-3F255931754F}
42 | EndGlobalSection
43 | EndGlobal
44 |
--------------------------------------------------------------------------------
/Base64/Base64.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net472;net48;net5.0;
4 | true
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Base64/Base64Buffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Security;
6 | using System.Security.Cryptography;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Base64
11 | {
12 | public class Base64Buffer
13 | {
14 | public Base64Buffer()
15 | {
16 | this.FromBase64Transform = new FromBase64Transform();
17 | this.FinalInputBlock = new byte[this.FromBase64Transform.InputBlockSize];
18 | this.Block = new Block(this.FromBase64Transform.InputBlockSize, this.FromBase64Transform.ChunkSize);
19 | }
20 |
21 | public FromBase64Transform FromBase64Transform { get; }
22 |
23 | public byte[] FinalInputBlock { get; }
24 |
25 | public Block Block { get; }
26 |
27 | public void Reset()
28 | {
29 | this.FromBase64Transform.Reset();
30 | this.Block.Reset();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Base64/Block.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Base64
8 | {
9 | public class Block
10 | {
11 | public byte[] input;
12 | public int inputOffset;
13 | public int inputCount;
14 |
15 | public byte[] outputBuffer;
16 | public int outputOffset;
17 |
18 | public Block(int inputBlockSize, int chunkSize)
19 | {
20 | this.input = new byte[inputBlockSize];
21 | this.outputBuffer = new byte[chunkSize];
22 | }
23 |
24 | public void Validate()
25 | {
26 | if (input == null)
27 | {
28 | throw new ArgumentNullException("input");
29 | }
30 |
31 | if (inputOffset < 0)
32 | {
33 | throw new ArgumentOutOfRangeException("inputOffset", "inputOffset must be positive");
34 | }
35 |
36 | if (inputCount < 0 || (inputCount > input.Length))
37 | {
38 | throw new ArgumentException("inputCount cannot be greater than input length");
39 | }
40 |
41 | if ((input.Length - inputCount) < inputOffset)
42 | {
43 | throw new ArgumentException("Invalid offset. offset + inputCount is beyond the input array bounds.");
44 | }
45 | }
46 |
47 | public void Reset()
48 | {
49 | this.inputOffset = this.inputCount = this.outputOffset = 0;
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Base64/BufferedStreamWriter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Base64
9 | {
10 | // https://referencesource.microsoft.com/#mscorlib/system/io/streamwriter.cs
11 | public class BufferedStreamWriter : TextWriter
12 | {
13 | private Encoding encoding;
14 | private Stream stream;
15 | private Encoder encoder;
16 |
17 | private byte[] byteBuffer;
18 | private char[] charBuffer;
19 |
20 | private int charLen;
21 | private int charPos;
22 | private bool closable;
23 | private bool autoFlush;
24 | private bool haveWrittenPreamble;
25 |
26 | public override Encoding Encoding => this.encoding;
27 |
28 | private bool LeaveOpen => !closable;
29 |
30 | public BufferedStreamWriter(Stream stream, EncodingBuffer encodingBuffer, bool leaveOpen = false)
31 | {
32 | this.stream = stream;
33 | this.encoding = encodingBuffer.Encoding;
34 | this.encoder = encodingBuffer.Encoder;
35 |
36 | this.charBuffer = encodingBuffer.CharBuffer;
37 | this.charLen = this.charBuffer.Length;
38 |
39 | this.byteBuffer = encodingBuffer.ByteBuffer;
40 |
41 | this.closable = !leaveOpen;
42 | }
43 |
44 | public override void Write(char value)
45 | {
46 | if (charPos == charLen) Flush(false, false);
47 | charBuffer[charPos] = value;
48 | charPos++;
49 | if (autoFlush) Flush(true, false);
50 | }
51 |
52 | public override void Write(char[] buffer)
53 | {
54 | // This may be faster than the one with the index & count since it
55 | // has to do less argument checking.
56 | if (buffer == null)
57 | return;
58 |
59 | int index = 0;
60 | int count = buffer.Length;
61 | while (count > 0)
62 | {
63 | if (charPos == charLen) Flush(false, false);
64 | int n = charLen - charPos;
65 | if (n > count) n = count;
66 | //Contract.Assert(n > 0, "StreamWriter::Write(char[]) isn't making progress! This is most likely a ---- in user code.");
67 | Buffer.BlockCopy(buffer, index * sizeof(char), charBuffer, charPos * sizeof(char), n * sizeof(char));
68 | charPos += n;
69 | index += n;
70 | count -= n;
71 | }
72 | if (autoFlush) Flush(true, false);
73 | }
74 |
75 | public override void Write(char[] buffer, int index, int count)
76 | {
77 | if (buffer == null)
78 | throw new ArgumentNullException("buffer");
79 | if (index < 0)
80 | throw new ArgumentOutOfRangeException("index");
81 | if (count < 0)
82 | throw new ArgumentOutOfRangeException("count");
83 | if (buffer.Length - index < count)
84 | throw new ArgumentException("Invalid index. index + count is beyond the buffer array bounds.");
85 |
86 | while (count > 0)
87 | {
88 | if (charPos == charLen) Flush(false, false);
89 | int n = charLen - charPos;
90 | if (n > count) n = count;
91 | Buffer.BlockCopy(buffer, index * sizeof(char), charBuffer, charPos * sizeof(char), n * sizeof(char));
92 | charPos += n;
93 | index += n;
94 | count -= n;
95 | }
96 | if (autoFlush) Flush(true, false);
97 | }
98 |
99 | public override void Write(String value)
100 | {
101 | if (value != null)
102 | {
103 | int count = value.Length;
104 | int index = 0;
105 | while (count > 0)
106 | {
107 | if (charPos == charLen) Flush(false, false);
108 | int n = charLen - charPos;
109 | if (n > count) n = count;
110 | value.CopyTo(index, charBuffer, charPos, n);
111 | charPos += n;
112 | index += n;
113 | count -= n;
114 | }
115 | if (autoFlush) Flush(true, false);
116 | }
117 | }
118 |
119 | protected override void Dispose(bool disposing)
120 | {
121 | try
122 | {
123 | // We need to flush any buffered data if we are being closed/disposed.
124 | // Also, we never close the handles for stdout & friends. So we can safely
125 | // write any buffered data to those streams even during finalization, which
126 | // is generally the right thing to do.
127 | if (stream != null)
128 | {
129 | // Note: flush on the underlying stream can throw (ex., low disk space)
130 | if (disposing)
131 | {
132 | Flush(true, true);
133 | }
134 | }
135 | }
136 | finally
137 | {
138 | // Dispose of our resources if this StreamWriter is closable.
139 | // Note: Console.Out and other such non closable streamwriters should be left alone
140 | if (!LeaveOpen && stream != null)
141 | {
142 | try
143 | {
144 | // Attempt to close the stream even if there was an IO error from Flushing.
145 | // Note that Stream.Close() can potentially throw here (may or may not be
146 | // due to the same Flush error). In this case, we still need to ensure
147 | // cleaning up internal resources, hence the finally block.
148 | if (disposing)
149 | stream.Close();
150 | }
151 | finally
152 | {
153 | stream = null;
154 | byteBuffer = null;
155 | charBuffer = null;
156 | encoding = null;
157 | encoder = null;
158 | charLen = 0;
159 | base.Dispose(disposing);
160 | }
161 | }
162 | }
163 | }
164 |
165 | public override void Flush()
166 | {
167 | Flush(true, true);
168 | }
169 |
170 | private void Flush(bool flushStream, bool flushEncoder)
171 | {
172 | // flushEncoder should be true at the end of the file and if
173 | // the user explicitly calls Flush (though not if AutoFlush is true).
174 | // This is required to flush any dangling characters from our UTF-7
175 | // and UTF-8 encoders.
176 | if (stream == null)
177 | throw new ObjectDisposedException("Object disposed, writer closed");
178 |
179 | // Perf boost for Flush on non-dirty writers.
180 | if (charPos == 0 && ((!flushStream && !flushEncoder)))
181 | return;
182 |
183 | if (!haveWrittenPreamble)
184 | {
185 | haveWrittenPreamble = true;
186 | byte[] preamble = encoding.GetPreamble();
187 | if (preamble.Length > 0)
188 | stream.Write(preamble, 0, preamble.Length);
189 | }
190 |
191 | int count = encoder.GetBytes(charBuffer, 0, charPos, byteBuffer, 0, flushEncoder);
192 | charPos = 0;
193 | if (count > 0)
194 | stream.Write(byteBuffer, 0, count);
195 | // By definition, calling Flush should flush the stream, but this is
196 | // only necessary if we passed in true for flushStream. The Web
197 | // Services guys have some perf tests where flushing needlessly hurts.
198 | if (flushStream)
199 | stream.Flush();
200 | }
201 | }
202 | }
203 |
--------------------------------------------------------------------------------
/Base64/EncodingBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Base64
8 | {
9 | public class EncodingBuffer
10 | {
11 | public EncodingBuffer(Encoding encoding, int bufferSize)
12 | {
13 | this.Encoding = encoding;
14 | this.Encoder = encoding.GetEncoder();
15 | this.CharBuffer = new char[bufferSize];
16 | this.ByteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)];
17 | }
18 |
19 | public Encoding Encoding { get; }
20 |
21 | public Encoder Encoder { get; }
22 |
23 | public byte[] ByteBuffer { get; }
24 |
25 | public char[] CharBuffer { get; }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Base64/FromBase64Transform.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.Linq;
5 | using System.Runtime.CompilerServices;
6 | using System.Security.Cryptography;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Base64
11 | {
12 | public class FromBase64Transform
13 | {
14 | // property access adds 0.13% according to profiler, every little helps :)
15 | private static readonly Encoding ASCII = Encoding.ASCII;
16 | const int bufferSize = 4096;
17 | private int tempCount;
18 |
19 | private byte[] tempByteBuffer = new byte[4];
20 | private char[] tempCharBuffer = new char[bufferSize];
21 |
22 | public FromBase64Transform()
23 | {
24 | }
25 |
26 | public int ChunkSize => bufferSize;
27 |
28 | // converting from Base64 generates 3 bytes output from each 4 bytes input block
29 | public int InputBlockSize
30 | {
31 | get { return 4; }
32 | }
33 |
34 | public int OutputBlockSize
35 | {
36 | get { return 3; }
37 | }
38 |
39 | // This will result in us allowing some invalid non-whitespace chars, but it is fast.
40 | // The invalid chars will be blocked later by UnsafeConvert.FromBase64CharArray
41 | const uint intSpace = (uint)' ';
42 |
43 | // This is optimized for the case where there are either no whitespace chars, or very few.
44 | // Try to process continuous arrays of data. If there is a whitespace, process everything before in a continuous array.
45 | // Then try to find a valid 4 byte section and process it. Then continue to find and process continuous arrays.
46 | public unsafe int TransformBlock(Block block)
47 | {
48 | block.Validate();
49 |
50 | int effectiveCount;
51 | int totalOutputBytes = 0;
52 |
53 | if (this.tempCount > 0)
54 | {
55 | totalOutputBytes += this.FlushTempBuffer(block);
56 | }
57 |
58 | fixed (byte* inputPtr = block.input)
59 | {
60 | byte* startPtr = (byte*)(inputPtr + block.inputOffset);
61 | byte* endMarkerPtr = (byte*)(inputPtr + block.inputOffset + block.inputCount);
62 | byte* endPtr = startPtr;
63 |
64 | while (endPtr < endMarkerPtr)
65 | {
66 | // find ptrs to a section of the array that doesn't contain spaces
67 | startPtr = SkipSpaces(startPtr, endMarkerPtr);
68 |
69 | // if we examined a section containing only whitespace, exit
70 | if (startPtr >= endMarkerPtr)
71 | {
72 | break;
73 | }
74 |
75 | endPtr = NextSpaceOrEnd(startPtr, endMarkerPtr);
76 |
77 | // Get the number of 4 bytes blocks to transform
78 | effectiveCount = (int)(endPtr - startPtr);
79 | int numBlocks = (effectiveCount) / 4;
80 |
81 | // Transform the bytes to chars and write to output
82 | int nWritten = 0;
83 | fixed (char* cp = tempCharBuffer)
84 | {
85 | nWritten = ASCII.GetChars(startPtr, 4 * numBlocks, cp, bufferSize);
86 | }
87 |
88 | fixed (byte* op = block.outputBuffer)
89 | {
90 | int outputBytes = UnsafeConvert.FromBase64CharArray(tempCharBuffer, 0, nWritten, op + block.outputOffset);
91 |
92 | block.outputOffset += outputBytes;
93 | totalOutputBytes += outputBytes;
94 | }
95 |
96 | // If we couldn't write a complete block, try to find 4 bytes starting from the last space.
97 | int remainder = effectiveCount % 4;
98 |
99 | if (remainder != 0)
100 | {
101 | // We need 4 bytes total, try to find remainder bytes after the whitespace
102 | byte* tmpPtr = startPtr + effectiveCount - remainder;
103 |
104 | int bytesFound = 0;
105 | while (tmpPtr < endMarkerPtr && bytesFound < 4)
106 | {
107 | uint c = (uint)(*tmpPtr);
108 |
109 | if (c > intSpace)
110 | this.tempByteBuffer[bytesFound++] = *tmpPtr;
111 |
112 | tmpPtr++;
113 | }
114 |
115 | if (bytesFound == 4)
116 | {
117 | // we have 4 bytes, write it to output
118 | fixed (char* cp = tempCharBuffer)
119 | fixed (byte* tp = this.tempByteBuffer)
120 | {
121 | nWritten = ASCII.GetChars(tp, 4, cp, bufferSize);
122 | }
123 |
124 | fixed (byte* op = block.outputBuffer)
125 | {
126 | int outputBytes = UnsafeConvert.FromBase64CharArray(tempCharBuffer, 0, nWritten, op + block.outputOffset);
127 |
128 | block.outputOffset += outputBytes;
129 | totalOutputBytes += outputBytes;
130 | }
131 |
132 | // advance startPtr past the block just written
133 | startPtr = tmpPtr;
134 | }
135 | else
136 | {
137 | // Must be the final block, mark remaining bytes in the temp buffer
138 | tempCount = bytesFound;
139 | break;
140 | }
141 | }
142 | else
143 | {
144 | startPtr = endPtr;
145 | }
146 | }
147 |
148 | return totalOutputBytes;
149 | }
150 | }
151 |
152 | public unsafe int TransformFinalBlock(Block block)
153 | {
154 | block.Validate();
155 |
156 | int totalOutputBytes = 0;
157 |
158 | // probably this becomes a sub method that can be run at the start of the other block method
159 | if (this.tempCount > 0)
160 | {
161 | totalOutputBytes = this.FlushTempBuffer(block);
162 | }
163 |
164 | if (block.inputCount == 0)
165 | {
166 | return totalOutputBytes;
167 | }
168 |
169 | if (block.inputCount < 4)
170 | {
171 | // handle trailing whitespace
172 | for (int i = block.inputOffset; i < block.inputOffset + block.inputCount; i++)
173 | {
174 | if (block.input[i] != (int)' ' && block.input[i] != (int)'\n' && block.input[i] != (int)'\r' && block.input[i] != (int)'\t' && block.input[i] != (int)'\0')
175 | {
176 | throw new FormatException("Invalid length for a Base-64 char array or string");
177 | }
178 | }
179 |
180 | return totalOutputBytes;
181 | }
182 |
183 | throw new InvalidOperationException("Too much data written in final block");
184 | }
185 |
186 | private unsafe int FlushTempBuffer(Block block)
187 | {
188 | int totalOutputBytes = 0;
189 |
190 | // try to write a full block starting with remainder of last write
191 | fixed (byte* inputPtr = block.input)
192 | {
193 | byte* startPtr = (byte*)(inputPtr + block.inputOffset);
194 | byte* endMarkerPtr = (byte*)(inputPtr + block.inputOffset + block.inputCount);
195 |
196 | startPtr = SkipSpaces(startPtr, endMarkerPtr);
197 |
198 | int bytesNeeded = 4 - this.tempCount;
199 |
200 | while (bytesNeeded > 0 && startPtr < endMarkerPtr)
201 | {
202 | uint c = (uint)(*startPtr);
203 |
204 | if (c > intSpace)
205 | {
206 | this.tempByteBuffer[this.tempCount++] = *startPtr;
207 | bytesNeeded--;
208 | }
209 |
210 | startPtr++;
211 | }
212 |
213 | if (bytesNeeded != 0)
214 | {
215 | throw new FormatException("Invalid base64 data length");
216 | }
217 |
218 | int nWritten = 0;
219 | this.tempCount = 0;
220 |
221 | // we have 4 bytes, write it to output
222 | fixed (char* cp = tempCharBuffer)
223 | fixed (byte* tp = this.tempByteBuffer)
224 | {
225 | nWritten = ASCII.GetChars(tp, 4, cp, bufferSize);
226 | }
227 |
228 | fixed (byte* op = block.outputBuffer)
229 | {
230 | int outputBytes = UnsafeConvert.FromBase64CharArray(tempCharBuffer, 0, nWritten, op + block.outputOffset);
231 |
232 | block.outputOffset += outputBytes;
233 | totalOutputBytes += outputBytes;
234 | }
235 |
236 | // advance offset
237 | int count = (int)(startPtr - (byte*)(inputPtr + block.inputOffset));
238 | block.inputOffset += count;
239 | block.inputCount -= count;
240 | }
241 |
242 | return totalOutputBytes;
243 | }
244 |
245 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
246 | private static unsafe byte* SkipSpaces(byte* startPtr, byte* endMarkerPtr)
247 | {
248 | while (startPtr < endMarkerPtr)
249 | {
250 | uint c = (uint)(*startPtr);
251 |
252 | if (c > intSpace)
253 | break;
254 |
255 | startPtr++;
256 | }
257 |
258 | return startPtr;
259 | }
260 |
261 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
262 | private static unsafe byte* NextSpaceOrEnd(byte* startPtr, byte* endMarkerPtr)
263 | {
264 | byte* endPtr = startPtr;
265 | while (endPtr < endMarkerPtr)
266 | {
267 | uint c = (uint)(*endPtr);
268 |
269 | if (c <= intSpace)
270 | break;
271 |
272 | endPtr++;
273 | }
274 |
275 | return endPtr;
276 | }
277 |
278 | public void Reset()
279 | {
280 | this.tempCount = 0;
281 | }
282 | }
283 | }
284 |
--------------------------------------------------------------------------------
/Base64/Infra/MemoryStreamFactory.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Base64
8 | {
9 | using System;
10 | using System.Collections.Generic;
11 | using System.IO;
12 | using System.Linq;
13 | using System.Text;
14 | using System.Threading.Tasks;
15 | using System.Web;
16 | using Microsoft.IO;
17 | using static Microsoft.IO.RecyclableMemoryStreamManager;
18 |
19 | public static class MemoryStreamFactory
20 | {
21 | private const int DefaultBlockSize = RecyclableMemoryStreamManager.DefaultBlockSize;
22 | private const int DefaultLargeBufferMultiple = 1 << 20;
23 | private const int DefaultMaximumBufferSize = 16 * (1 << 20); // 16mb
24 |
25 | private static RecyclableMemoryStreamManager streamManager = CreateStreamManager();
26 |
27 | private const int reportFrequency = 10000;
28 | private static long sampleCount = 0;
29 |
30 | public static MemoryStream Create(string tag)
31 | {
32 | if (UseRecycledMemoryStream)
33 | {
34 | return streamManager.GetStream(tag);
35 | }
36 |
37 | return new MemoryStream();
38 | }
39 |
40 | private static RecyclableMemoryStreamManager CreateStreamManager()
41 | {
42 | var rmsm = new RecyclableMemoryStreamManager(
43 | DefaultBlockSize,
44 | DefaultLargeBufferMultiple,
45 | DefaultMaximumBufferSize,
46 | useExponentialLargeBuffer: true)
47 | {
48 | // Calling stream.GetBuffer/TryGetBuffer can switch from multiple small buffers to 1 large buffer.
49 | // This setting forces the small buffers to be returned to the pool immediately once a stream switches
50 | // to large buffer mode.
51 | AggressiveBufferReturn = true,
52 | };
53 |
54 |
55 | return rmsm;
56 | }
57 |
58 | private static bool UseRecycledMemoryStream = true;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/Base64/Infra/ObjectPool.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading;
7 | using System.Threading.Tasks;
8 |
9 | namespace Base64
10 | {
11 | public class ObjectPool
12 | where T : class
13 | {
14 | [DebuggerDisplay("{Value,nq}")]
15 | private struct Element
16 | {
17 | internal T Value;
18 | }
19 |
20 | ///
21 | /// Not using System.Func{T} because this file is linked into the (debugger) Formatter,
22 | /// which does not have that type (since it compiles against .NET 2.0).
23 | ///
24 | public delegate T Factory();
25 |
26 | // Storage for the pool objects. The first item is stored in a dedicated field because we
27 | // expect to be able to satisfy most requests from it.
28 | private T _firstItem;
29 | private readonly Element[] _items;
30 |
31 | // factory is stored for the lifetime of the pool. We will call this only when pool needs to
32 | // expand. compared to "new T()", Func gives more flexibility to implementers and faster
33 | // than "new T()".
34 | private readonly Factory _factory;
35 |
36 | #if DETECT_LEAKS
37 | private static readonly ConditionalWeakTable leakTrackers = new ConditionalWeakTable();
38 |
39 | private class LeakTracker : IDisposable
40 | {
41 | private volatile bool disposed;
42 |
43 | #if TRACE_LEAKS
44 | internal volatile object Trace = null;
45 | #endif
46 |
47 | public void Dispose()
48 | {
49 | disposed = true;
50 | GC.SuppressFinalize(this);
51 | }
52 |
53 | private string GetTrace()
54 | {
55 | #if TRACE_LEAKS
56 | return Trace == null ? "" : Trace.ToString();
57 | #else
58 | return "Leak tracing information is disabled. Define TRACE_LEAKS on ObjectPool`1.cs to get more info \n";
59 | #endif
60 | }
61 |
62 | ~LeakTracker()
63 | {
64 | if (!this.disposed && !Environment.HasShutdownStarted)
65 | {
66 | var trace = GetTrace();
67 |
68 | // If you are seeing this message it means that object has been allocated from the pool
69 | // and has not been returned back. This is not critical, but turns pool into rather
70 | // inefficient kind of "new".
71 | Debug.WriteLine($"TRACEOBJECTPOOLLEAKS_BEGIN\nPool detected potential leaking of {typeof(T)}. \n Location of the leak: \n {GetTrace()} TRACEOBJECTPOOLLEAKS_END");
72 | }
73 | }
74 | }
75 | #endif
76 |
77 | public ObjectPool(Factory factory)
78 | : this(factory, Environment.ProcessorCount * 2)
79 | {
80 | }
81 |
82 | public ObjectPool(Factory factory, int size)
83 | {
84 | Debug.Assert(size >= 1);
85 | _factory = factory;
86 | _items = new Element[size - 1];
87 | }
88 |
89 | private T CreateInstance()
90 | {
91 | var inst = _factory();
92 | return inst;
93 | }
94 |
95 | ///
96 | /// Produces an instance.
97 | ///
98 | ///
99 | /// Search strategy is a simple linear probing which is chosen for it cache-friendliness.
100 | /// Note that Free will try to store recycled objects close to the start thus statistically
101 | /// reducing how far we will typically search.
102 | ///
103 | public T Allocate()
104 | {
105 | // PERF: Examine the first element. If that fails, AllocateSlow will look at the remaining elements.
106 | // Note that the initial read is optimistically not synchronized. That is intentional.
107 | // We will interlock only when we have a candidate. in a worst case we may miss some
108 | // recently returned objects. Not a big deal.
109 | T inst = _firstItem;
110 | if (inst == null || inst != Interlocked.CompareExchange(ref _firstItem, null, inst))
111 | {
112 | inst = AllocateSlow();
113 | }
114 |
115 | #if DETECT_LEAKS
116 | var tracker = new LeakTracker();
117 | leakTrackers.Add(inst, tracker);
118 |
119 | #if TRACE_LEAKS
120 | var frame = CaptureStackTrace();
121 | tracker.Trace = frame;
122 | #endif
123 | #endif
124 | return inst;
125 | }
126 |
127 | private T AllocateSlow()
128 | {
129 | var items = _items;
130 |
131 | for (int i = 0; i < items.Length; i++)
132 | {
133 | // Note that the initial read is optimistically not synchronized. That is intentional.
134 | // We will interlock only when we have a candidate. in a worst case we may miss some
135 | // recently returned objects. Not a big deal.
136 | T inst = items[i].Value;
137 | if (inst != null)
138 | {
139 | if (inst == Interlocked.CompareExchange(ref items[i].Value, null, inst))
140 | {
141 | return inst;
142 | }
143 | }
144 | }
145 |
146 | return CreateInstance();
147 | }
148 |
149 | ///
150 | /// Returns objects to the pool.
151 | ///
152 | ///
153 | /// Search strategy is a simple linear probing which is chosen for it cache-friendliness.
154 | /// Note that Free will try to store recycled objects close to the start thus statistically
155 | /// reducing how far we will typically search in Allocate.
156 | ///
157 | public void Free(T obj)
158 | {
159 | Validate(obj);
160 | ForgetTrackedObject(obj);
161 |
162 | if (_firstItem == null)
163 | {
164 | // Intentionally not using interlocked here.
165 | // In a worst case scenario two objects may be stored into same slot.
166 | // It is very unlikely to happen and will only mean that one of the objects will get collected.
167 | _firstItem = obj;
168 | }
169 | else
170 | {
171 | FreeSlow(obj);
172 | }
173 | }
174 |
175 | private void FreeSlow(T obj)
176 | {
177 | var items = _items;
178 | for (int i = 0; i < items.Length; i++)
179 | {
180 | if (items[i].Value == null)
181 | {
182 | // Intentionally not using interlocked here.
183 | // In a worst case scenario two objects may be stored into same slot.
184 | // It is very unlikely to happen and will only mean that one of the objects will get collected.
185 | items[i].Value = obj;
186 | break;
187 | }
188 | }
189 | }
190 |
191 | ///
192 | /// Removes an object from leak tracking.
193 | ///
194 | /// This is called when an object is returned to the pool. It may also be explicitly
195 | /// called if an object allocated from the pool is intentionally not being returned
196 | /// to the pool. This can be of use with pooled arrays if the consumer wants to
197 | /// return a larger array to the pool than was originally allocated.
198 | ///
199 | [Conditional("DEBUG")]
200 | public void ForgetTrackedObject(T old, T replacement = null)
201 | {
202 | #if DETECT_LEAKS
203 | LeakTracker tracker;
204 | if (leakTrackers.TryGetValue(old, out tracker))
205 | {
206 | tracker.Dispose();
207 | leakTrackers.Remove(old);
208 | }
209 | else
210 | {
211 | var trace = CaptureStackTrace();
212 | Debug.WriteLine($"TRACEOBJECTPOOLLEAKS_BEGIN\nObject of type {typeof(T)} was freed, but was not from pool. \n Callstack: \n {trace} TRACEOBJECTPOOLLEAKS_END");
213 | }
214 |
215 | if (replacement != null)
216 | {
217 | tracker = new LeakTracker();
218 | leakTrackers.Add(replacement, tracker);
219 | }
220 | #endif
221 | }
222 |
223 | #if DETECT_LEAKS
224 | private static Lazy _stackTraceType = new Lazy(() => Type.GetType("System.Diagnostics.StackTrace"));
225 |
226 | private static object CaptureStackTrace()
227 | {
228 | return Activator.CreateInstance(_stackTraceType.Value);
229 | }
230 | #endif
231 |
232 | [Conditional("DEBUG")]
233 | private void Validate(object obj)
234 | {
235 | Debug.Assert(obj != null, "freeing null?");
236 |
237 | Debug.Assert(_firstItem != obj, "freeing twice?");
238 |
239 | var items = _items;
240 | for (int i = 0; i < items.Length; i++)
241 | {
242 | var value = items[i].Value;
243 | if (value == null)
244 | {
245 | return;
246 | }
247 |
248 | Debug.Assert(value != obj, "freeing twice?");
249 | }
250 | }
251 | }
252 | }
253 |
--------------------------------------------------------------------------------
/Base64/MemoryStreamExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Security.Cryptography;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Base64
10 | {
11 | public static class MemoryStreamExtensions
12 | {
13 | ///
14 | /// Utility method to replace new MemoryStream(Encoding.UTF8.GetBytes(str)) with a direct stream write.
15 | ///
16 | /// The stream to write to
17 | /// The string to write
18 | /// The stream writer used
19 | public static StreamWriter WriteUtf8AndSetStart(this MemoryStream stream, string str)
20 | {
21 | var writer = new StreamWriter(stream, NoBomEncoding.UTF8);
22 | writer.Write(str);
23 | writer.Flush();
24 | stream.Position = 0;
25 |
26 | return writer;
27 | }
28 |
29 | public static string ReadUtf8(this MemoryStream stream)
30 | {
31 | stream.TryGetBuffer(out var buffer);
32 | return Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);
33 | }
34 |
35 | public static void WriteFromBase64(this Stream stream, string base64)
36 | {
37 | // this should actually be ASCII, but UTF8 is faster.
38 | var utf8Buffer = PooledUtf8EncodingBuffer.GetInstance();
39 | var base64Buffer = PooledBase64Buffer.GetInstance();
40 |
41 | try
42 | {
43 | using (var base64Stream = new TransformBase64Stream(stream, base64Buffer, true))
44 | {
45 | using (var writer = new BufferedStreamWriter(base64Stream, utf8Buffer, true))
46 | {
47 | writer.Write(base64);
48 | writer.Flush();
49 | }
50 | }
51 | }
52 | finally
53 | {
54 | PooledUtf8EncodingBuffer.Free(utf8Buffer);
55 | PooledBase64Buffer.Free(base64Buffer);
56 | }
57 | }
58 |
59 | public static string ReadToBase64(this MemoryStream stream)
60 | {
61 | stream.TryGetBuffer(out var buffer);
62 | return Convert.ToBase64String(buffer.Array, buffer.Offset, buffer.Count);
63 | }
64 | }
65 |
66 | public static class NoBomEncoding
67 | {
68 | public static readonly Encoding ASCII = new ASCIIEncoding();
69 | public static readonly Encoding UTF8 = new UTF8Encoding(false);
70 | public static readonly Encoding Unicode = new UnicodeEncoding(false, false);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/Base64/PooledBase64Buffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Base64
8 | {
9 | public class PooledBase64Buffer
10 | {
11 | private static readonly ObjectPool PoolInstance = CreatePool();
12 |
13 | public const int BufferSize = 4096;
14 |
15 | private static ObjectPool CreatePool()
16 | {
17 | return new ObjectPool(() => new Base64Buffer());
18 | }
19 |
20 | public static Base64Buffer GetInstance()
21 | {
22 | return PoolInstance.Allocate();
23 | }
24 |
25 | public static void Free(Base64Buffer buffer)
26 | {
27 | buffer.Reset();
28 |
29 | PoolInstance.Free(buffer);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Base64/PooledUtf8EncodingBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Base64
8 | {
9 | public class PooledUtf8EncodingBuffer
10 | {
11 | private static readonly ObjectPool PoolInstance = CreatePool();
12 |
13 | public const int BufferSize = 4096;
14 |
15 | private static ObjectPool CreatePool()
16 | {
17 | return new ObjectPool(() => new EncodingBuffer(NoBomEncoding.UTF8, BufferSize));
18 | }
19 |
20 | public static EncodingBuffer GetInstance()
21 | {
22 | return PoolInstance.Allocate();
23 | }
24 |
25 | public static void Free(EncodingBuffer builder)
26 | {
27 | builder.Encoder.Reset();
28 |
29 | PoolInstance.Free(builder);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Base64/StringExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Security.Cryptography;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Base64
9 | {
10 | public static class StringExtensions
11 | {
12 | ///
13 | /// Convert large strings to UTF8 base 64 without allocating an intermediate byte array.
14 | ///
15 | /// The input string
16 | /// The input string encoded as base64
17 | public static string ToUtf8Base64String(this string input)
18 | {
19 | // default method is faster for small strings
20 | if (input.Length < 160)
21 | {
22 | var b = Encoding.UTF8.GetBytes(input);
23 | return Convert.ToBase64String(b);
24 | }
25 |
26 | var utf8Buffer = PooledUtf8EncodingBuffer.GetInstance();
27 |
28 | try
29 | {
30 | // write a stream containing the string (now the stream is a byte[] equivalent to Encoding.X.GetBytes(test))
31 | // Note that if the encoding has BOM, streamwriter will not match Encoding.X.GetBytes(test)
32 | using (var s = MemoryStreamFactory.Create(nameof(ToUtf8Base64String)))
33 | {
34 | using (var w = new BufferedStreamWriter(s, utf8Buffer, true))
35 | {
36 | w.Write(input);
37 | w.Flush();
38 | }
39 |
40 | s.Position = 0;
41 | return s.ReadToBase64();
42 | }
43 | }
44 | finally
45 | {
46 | PooledUtf8EncodingBuffer.Free(utf8Buffer);
47 | }
48 | }
49 |
50 | public static string FromUtf8Base64String(this string base64)
51 | {
52 | if (base64.Length < 1024)
53 | {
54 | var bytes = Convert.FromBase64String(base64);
55 | return Encoding.UTF8.GetString(bytes);
56 | }
57 |
58 | // this should actually be ASCII, but UTF8 is faster.
59 | var utf8Buffer = PooledUtf8EncodingBuffer.GetInstance();
60 | var base64Buffer = PooledBase64Buffer.GetInstance();
61 |
62 | try
63 | {
64 | using (var s = MemoryStreamFactory.Create(nameof(FromUtf8Base64String)))
65 | {
66 | using (var base64Stream = new TransformBase64Stream(s, base64Buffer, true))
67 | {
68 | using (var writer = new BufferedStreamWriter(base64Stream, utf8Buffer, true))
69 | {
70 | writer.Write(base64);
71 | writer.Flush();
72 | }
73 | }
74 |
75 | s.TryGetBuffer(out var buffer);
76 | return Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);
77 | }
78 | }
79 | finally
80 | {
81 | PooledUtf8EncodingBuffer.Free(utf8Buffer);
82 | PooledBase64Buffer.Free(base64Buffer);
83 | }
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/Base64/TransformBase64Stream.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Base64
9 | {
10 | // https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/cryptostream.cs,aeb692cbbaf22679
11 | public class TransformBase64Stream : Stream
12 | {
13 | private readonly Stream stream;
14 | private readonly FromBase64Transform transform;
15 |
16 | private byte[] finalInputBlock;
17 | private int finalInputBlockCount = 0;
18 | private int finalInputBlockSize;
19 | private bool finalBlockTransformed = false;
20 | private bool leaveOpen;
21 |
22 | private Block block;
23 |
24 | private const string StreamNotSeekable = "Stream is not seekable";
25 |
26 | public TransformBase64Stream(Stream stream, Base64Buffer base64Buffer, bool leaveOpen = false)
27 | {
28 | this.stream = stream;
29 | this.transform = base64Buffer.FromBase64Transform;
30 |
31 | this.finalInputBlockSize = base64Buffer.FinalInputBlock.Length;
32 | this.finalInputBlock = base64Buffer.FinalInputBlock;
33 |
34 | this.block = base64Buffer.Block;
35 |
36 | this.leaveOpen = leaveOpen;
37 | }
38 |
39 | public bool HasFlushedFinalBlock
40 | {
41 | get { return this.finalBlockTransformed; }
42 | }
43 |
44 | public override bool CanRead => false;
45 |
46 | public override bool CanSeek => false;
47 |
48 | public override bool CanWrite => true;
49 |
50 | public override long Length => throw new NotSupportedException(StreamNotSeekable);
51 |
52 | public override long Position { get => throw new NotSupportedException(StreamNotSeekable); set => throw new NotSupportedException(StreamNotSeekable); }
53 |
54 | public void FlushFinalBlock()
55 | {
56 | if (this.finalBlockTransformed)
57 | throw new NotSupportedException("Final block already flushed");
58 |
59 | // We have to process the last block here. First, we have the final block in tempInputBlock, so transform it
60 | this.block.input = this.finalInputBlock;
61 | this.block.inputOffset = 0;
62 | this.block.inputCount = this.finalInputBlockCount;
63 | this.block.outputOffset = 0;
64 |
65 | int len = this.transform.TransformFinalBlock(this.block);
66 | this.block.Reset();
67 |
68 | this.finalBlockTransformed = true;
69 |
70 | this.stream.Write(this.block.outputBuffer, 0, len);
71 |
72 | this.stream.Flush();
73 |
74 | return;
75 | }
76 |
77 | public override void Flush()
78 | {
79 | return;
80 | }
81 |
82 | public override int Read(byte[] buffer, int offset, int count)
83 | {
84 | throw new NotSupportedException("Stream is write only");
85 | }
86 |
87 | public override long Seek(long offset, SeekOrigin origin)
88 | {
89 | throw new NotSupportedException(StreamNotSeekable);
90 | }
91 |
92 | public override void SetLength(long value)
93 | {
94 | throw new NotSupportedException(StreamNotSeekable);
95 | }
96 |
97 | public override void Write(byte[] buffer, int offset, int count)
98 | {
99 | if (offset < 0)
100 | throw new ArgumentOutOfRangeException("offset", "ArgumentOutOfRange_NeedNonNegNum");
101 | if (count < 0)
102 | throw new ArgumentOutOfRangeException("count", "ArgumentOutOfRange_NeedNonNegNum");
103 | if (buffer.Length - offset < count)
104 | throw new ArgumentException("Argument_InvalidOffLen");
105 |
106 | if (this.finalInputBlockCount != 0)
107 | throw new InvalidOperationException("An incomplete block was written prior to calling write.");
108 |
109 | // write <= count bytes to the output stream, transforming as we go.
110 | // Basic idea: using bytes in the inputBlock first, make whole blocks,
111 | // transform them, and write them out. Cache any remaining bytes in the finalInputBlock.
112 | int bytesToWrite = count;
113 | int currentInputIndex = offset;
114 |
115 | int numOutputBytes;
116 |
117 | while (bytesToWrite > 0)
118 | {
119 | if (bytesToWrite >= this.finalInputBlockSize)
120 | {
121 | // take chunks of outputBuffer.Length
122 | int chunk = Math.Min(this.block.outputBuffer.Length, bytesToWrite);
123 |
124 | // only write whole blocks
125 | int numWholeBlocks = chunk / finalInputBlockSize;
126 | int numWholeBlocksInBytes = numWholeBlocks * finalInputBlockSize;
127 |
128 | this.block.input = buffer;
129 | this.block.inputOffset = currentInputIndex;
130 | this.block.inputCount = numWholeBlocksInBytes;
131 | this.block.outputOffset = 0;
132 |
133 | numOutputBytes = this.transform.TransformBlock(block);
134 | this.stream.Write(this.block.outputBuffer, 0, numOutputBytes);
135 | currentInputIndex += numWholeBlocksInBytes;
136 | bytesToWrite -= numWholeBlocksInBytes;
137 | }
138 | else
139 | {
140 | // In this case, we don't have an entire block's worth left, so store it up in the
141 | // input buffer, which by now must be empty.
142 | Buffer.BlockCopy(buffer, currentInputIndex, this.finalInputBlock, 0, bytesToWrite);
143 | this.finalInputBlockCount += bytesToWrite;
144 |
145 | return;
146 | }
147 | }
148 |
149 | return;
150 | }
151 |
152 | protected override void Dispose(bool disposing)
153 | {
154 | try
155 | {
156 | if (disposing)
157 | {
158 | if (!this.finalBlockTransformed)
159 | {
160 | this.FlushFinalBlock();
161 | }
162 |
163 | if (!leaveOpen)
164 | {
165 | this.stream.Close();
166 | }
167 | }
168 | }
169 | finally
170 | {
171 | try
172 | {
173 | // Ensure we don't try to transform the final block again if we get disposed twice
174 | // since it's null after this
175 | this.finalBlockTransformed = true;
176 |
177 | this.block = null;
178 | this.finalInputBlock = null;
179 | }
180 | finally
181 | {
182 | base.Dispose(disposing);
183 | }
184 | }
185 | }
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/Base64/UnsafeConvert.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Security;
6 | using System.Text;
7 |
8 | namespace Base64
9 | {
10 | public static class UnsafeConvert
11 | {
12 | ///
13 | /// Converts the specified range of a Char array, which encodes binary data as Base64 digits, to the equivalent byte array.
14 | ///
15 | /// Chars representing Base64 encoding characters
16 | /// A position within the input array.
17 | /// Number of element to convert.
18 | /// The array of bytes represented by the specified Base64 encoding characters.
19 | [SecuritySafeCritical]
20 | public static unsafe int FromBase64CharArray(char[] inArray, int offset, int length, byte* destination)
21 | {
22 | if (inArray == null)
23 | throw new ArgumentNullException("inArray");
24 |
25 | if (length < 0)
26 | throw new ArgumentOutOfRangeException("length", "length must be positive.");
27 |
28 | if (offset < 0)
29 | throw new ArgumentOutOfRangeException("offset", "offset must be positive.");
30 |
31 | if (offset > inArray.Length - length)
32 | throw new ArgumentOutOfRangeException("offset", "offset is too long");
33 |
34 | fixed (char* inArrayPtr = inArray)
35 | {
36 | return FromBase64CharPtr(inArrayPtr + offset, length, destination);
37 | }
38 | }
39 |
40 | ///
41 | /// Convert Base64 encoding characters to bytes:
42 | /// - Compute result length
43 | /// - Decode input into the specified array;
44 | ///
45 | /// Pointer to the first input char
46 | /// Number of input chars
47 | ///
48 | [SecurityCritical]
49 | private static unsafe int FromBase64CharPtr(char* inputPtr, int inputLength, byte* decodedBytesPtr)
50 | {
51 | int resultLength = (inputLength / 4) * 3;
52 |
53 | // resultLength can be zero. We will still enter FromBase64_Decode and process the input.
54 | // It may either simply write no bytes (e.g. input = " ") or throw (e.g. input = "ab").
55 |
56 | // Convert Base64 chars into bytes:
57 | return FromBase64_DecodeReflect(inputPtr, inputLength, decodedBytesPtr, resultLength);
58 |
59 | // Note that actualResultLength can differ from resultLength if the caller is modifying the array
60 | // as it is being converted. Silently ignore the failure.
61 | // Consider throwing exception in an non in-place release.
62 | }
63 |
64 | private static readonly MethodInfo FromBase64_DecodeMethodInfo = typeof(Convert).GetMethod("FromBase64_Decode", BindingFlags.Static | BindingFlags.NonPublic);
65 |
66 | // no measurable overhead from reflection here - it's not in a tight loop.
67 | // Can we turn this into a func to avoid allocating the args array? Pointers still need to be boxed.
68 | public static unsafe int FromBase64_DecodeReflect(
69 | char* startInputPtr,
70 | int inputLength,
71 | byte* startDestPtr,
72 | int destLength)
73 | {
74 | var inputPtr = Pointer.Box(startInputPtr, typeof(char*));
75 | var destPtr = Pointer.Box(startDestPtr, typeof(byte*));
76 |
77 | var args = new object[] { inputPtr, inputLength, destPtr, destLength };
78 |
79 | try
80 | {
81 | return (int)FromBase64_DecodeMethodInfo.Invoke(null, args);
82 | }
83 | catch (TargetInvocationException ex)
84 | {
85 | // unwrap
86 | throw ex.InnerException ?? ex;
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Benchmark/Benchmark.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net472;net48;net5.0;
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Benchmark/Program.cs:
--------------------------------------------------------------------------------
1 | using Benchmark.Stream;
2 | using BenchmarkDotNet.Configs;
3 | using BenchmarkDotNet.Jobs;
4 | using BenchmarkDotNet.Running;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace Benchmark
12 | {
13 | class Program
14 | {
15 | static void Main(string[] args)
16 | {
17 | BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Benchmark/Stream/StreamFromBase64.cs:
--------------------------------------------------------------------------------
1 | using Base64;
2 | using BenchmarkDotNet.Attributes;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Benchmark.Stream
11 | {
12 | [MemoryDiagnoser]
13 | public class StreamFromBase64
14 | {
15 | private static Data[] data = Setup();
16 |
17 | const int count = 10;
18 |
19 | private static Data[] Setup()
20 | {
21 | var d = new Data[count];
22 |
23 | for (int i = 0; i < count; i++)
24 | {
25 | int pow = (int)Math.Pow(2, i+8);
26 | var s = new string('a', pow);
27 | var ms = new MemoryStream();
28 |
29 | s = s.ToUtf8Base64String();
30 |
31 | d[i] = new Data()
32 | {
33 | Stream = ms,
34 | String = s,
35 | Count = pow
36 | };
37 | }
38 |
39 | return d;
40 | }
41 |
42 | [Benchmark(Baseline = true)]
43 | [ArgumentsSource(nameof(Input))]
44 | public void ConvertFrom(Data input)
45 | {
46 | input.Stream.Position = 0;
47 |
48 | byte[] decodedBytes = Convert.FromBase64String(input.String);
49 | Encoding.UTF8.GetString(decodedBytes);
50 | }
51 |
52 | [Benchmark]
53 | [ArgumentsSource(nameof(Input))]
54 | public void StreamFrom(Data input)
55 | {
56 | input.Stream.Position = 0;
57 | input.Stream.WriteFromBase64(input.String);
58 | }
59 |
60 |
61 | public static IEnumerable Input()
62 | {
63 | for (int i = 0; i < count; i++)
64 | yield return data[i];
65 | }
66 |
67 | public class Data
68 | {
69 | public MemoryStream Stream { get; set; }
70 |
71 | public string String { get; set; }
72 |
73 | public int Count { get; set; }
74 |
75 | public override string ToString()
76 | {
77 | return Count.ToString();
78 | }
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/Benchmark/Stream/StreamToBase64.cs:
--------------------------------------------------------------------------------
1 | using Base64;
2 | using BenchmarkDotNet.Attributes;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Diagnostics.PerformanceData;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace Benchmark.Stream
12 | {
13 | [MemoryDiagnoser]
14 | public class StreamToBase64
15 | {
16 | private static Data[] data = Setup();
17 | const int count = 10;
18 |
19 | private static Data[] Setup()
20 | {
21 | var d = new Data[count];
22 |
23 | for (int i = 0; i < count; i++)
24 | {
25 | int pow = (int)Math.Pow(2, i+8);
26 | var s = new string('a', pow);
27 | var ms = new MemoryStream();
28 | ms.WriteUtf8AndSetStart(s);
29 |
30 | d[i] = new Data()
31 | {
32 | Stream = ms,
33 | String = s,
34 | Count = pow
35 | };
36 | }
37 |
38 | return d;
39 | }
40 |
41 | [Benchmark(Baseline = true)]
42 | [ArgumentsSource(nameof(Input))]
43 | public string ConvertTo(Data input)
44 | {
45 | input.Stream.Position = 0;
46 |
47 | var b = Encoding.UTF8.GetBytes(input.String);
48 | return Convert.ToBase64String(b);
49 | }
50 |
51 | [Benchmark]
52 | [ArgumentsSource(nameof(Input))]
53 | public string StreamTo(Data input)
54 | {
55 | input.Stream.Position = 0;
56 | return input.Stream.ReadToBase64();
57 | }
58 |
59 |
60 | public static IEnumerable Input()
61 | {
62 | for (int i = 0; i < count; i++)
63 | yield return data[i];
64 | }
65 |
66 | public class Data
67 | {
68 | public MemoryStream Stream { get; set; }
69 |
70 | public string String { get; set; }
71 |
72 | public int Count { get; set; }
73 |
74 | public override string ToString()
75 | {
76 | return Count.ToString();
77 | }
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/Benchmark/String/StringFromBase64.cs:
--------------------------------------------------------------------------------
1 | using Base64;
2 | using BenchmarkDotNet.Attributes;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Security.Cryptography;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Benchmark
11 | {
12 | [MemoryDiagnoser]
13 | public class StringFromBase64
14 | {
15 | private static Data[] data = Setup();
16 | const int count = 10;
17 |
18 | private static Data[] Setup()
19 | {
20 | var d = new Data[count];
21 |
22 | for (int i = 0; i < count; i++)
23 | {
24 | int pow = (int)Math.Pow(2, i + 8);
25 |
26 | d[i] = new Data(pow);
27 | }
28 |
29 | return d;
30 | }
31 |
32 | // StreamTo memory alloc should be something like ConvertTo - GetBytes, since GetBytes should be done via the pooled objects.
33 | // Need to run under memory profiler and see where allocs come from.
34 |
35 | [Benchmark(Baseline = true)]
36 | [ArgumentsSource(nameof(Length))]
37 | public string ConvertFrom(Data data)
38 | {
39 | var r = Convert.FromBase64String(data.String);
40 | return Encoding.UTF8.GetString(r);
41 | }
42 |
43 | [Benchmark]
44 | [ArgumentsSource(nameof(Length))]
45 | public string StreamFrom(Data data)
46 | {
47 | return data.String.FromUtf8Base64String();
48 | }
49 |
50 | //[Benchmark]
51 | //[ArgumentsSource(nameof(Length))]
52 | //public string CryptoStreamFrom(Data data)
53 | //{
54 | // return CryptoFromUtf8Base64String(data.String);
55 | //}
56 |
57 | //[Benchmark]
58 | //[ArgumentsSource(nameof(Length))]
59 | //public byte[] GetBytes(Data data)
60 | //{
61 | // return Encoding.UTF8.GetBytes(data.String);
62 | //}
63 |
64 | // just to demonstrate how bad crypto version is
65 | public static string CryptoFromUtf8Base64String(string base64)
66 | {
67 | var encodingBuffer = PooledUtf8EncodingBuffer.GetInstance();
68 |
69 | try
70 | {
71 | using (var s = MemoryStreamFactory.Create(nameof(CryptoFromUtf8Base64String)))
72 | using (CryptoStream base64Stream = new CryptoStream(s, new System.Security.Cryptography.FromBase64Transform(), CryptoStreamMode.Write))
73 | {
74 | using (var writer = new BufferedStreamWriter(base64Stream, encodingBuffer, true))
75 | {
76 | writer.Write(base64);
77 | writer.Flush();
78 | }
79 |
80 | base64Stream.FlushFinalBlock();
81 |
82 | s.TryGetBuffer(out var buffer);
83 | return encodingBuffer.Encoding.GetString(buffer.Array, buffer.Offset, buffer.Count);
84 | }
85 | }
86 | finally
87 | {
88 | PooledUtf8EncodingBuffer.Free(encodingBuffer);
89 | }
90 | }
91 |
92 | public static IEnumerable Length()
93 | {
94 | for (int i = 0; i < count; i++)
95 | yield return data[i];
96 | }
97 |
98 | public class Data
99 | {
100 | public Data(int count)
101 | {
102 | this.String = new string('a', count).ToUtf8Base64String();
103 | this.Count = count;
104 | }
105 |
106 | public string String { get; set; }
107 |
108 | public int Count { get; set; }
109 |
110 | public override string ToString()
111 | {
112 | return Count.ToString();
113 | }
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/Benchmark/String/StringFromBase64Loh.cs:
--------------------------------------------------------------------------------
1 | using Base64;
2 | using BenchmarkDotNet.Attributes;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Benchmark
10 | {
11 | [MemoryDiagnoser]
12 | public class StringFromBase64Loh
13 | {
14 | private static readonly string lohString = new string('a', 500000).ToUtf8Base64String();
15 |
16 | [GlobalSetup]
17 | public static void Setup()
18 | {
19 | lohString.FromUtf8Base64String();
20 | }
21 |
22 | [Benchmark(Baseline = true)]
23 | public string ConvertFrom()
24 | {
25 | var r = Convert.FromBase64String(lohString);
26 | return Encoding.UTF8.GetString(r);
27 | }
28 |
29 | [Benchmark]
30 | public string StreamFrom()
31 | {
32 | return lohString.FromUtf8Base64String();
33 | }
34 |
35 | [Benchmark]
36 | public byte[] GetBytes()
37 | {
38 | return Encoding.UTF8.GetBytes(lohString);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Benchmark/String/StringToBase64.cs:
--------------------------------------------------------------------------------
1 | using Base64;
2 | using BenchmarkDotNet.Attributes;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Benchmark
10 | {
11 | [MemoryDiagnoser]
12 | public class StringToBase64
13 | {
14 | private static Data[] data = Setup();
15 | const int count = 10;
16 |
17 | private static Data[] Setup()
18 | {
19 | var d = new Data[count];
20 |
21 | for (int i = 0; i < count; i++)
22 | {
23 | int pow = (int)Math.Pow(2, i + 8);
24 |
25 | d[i] = new Data(pow);
26 | }
27 |
28 | return d;
29 | }
30 |
31 | [Benchmark(Baseline = true)]
32 | [ArgumentsSource(nameof(Length))]
33 | public string ConvertTo(Data data)
34 | {
35 | var b = Encoding.UTF8.GetBytes(data.String);
36 | return Convert.ToBase64String(b);
37 | }
38 |
39 | [Benchmark]
40 | [ArgumentsSource(nameof(Length))]
41 | public string StreamTo(Data data)
42 | {
43 | return data.String.ToUtf8Base64String();
44 | }
45 |
46 | //[Benchmark]
47 | //[ArgumentsSource(nameof(Length))]
48 | //public byte[] GetBytes(Data data)
49 | //{
50 | // return Encoding.UTF8.GetBytes(data.String);
51 | //}
52 |
53 | public static IEnumerable Length()
54 | {
55 | for (int i = 0; i < count; i++)
56 | yield return data[i];
57 | }
58 |
59 | public class Data
60 | {
61 | public Data(int count)
62 | {
63 | this.String = new string('a', count);
64 | this.Count = count;
65 | }
66 |
67 | public string String { get; set; }
68 |
69 | public int Count { get; set; }
70 |
71 | public override string ToString()
72 | {
73 | return Count.ToString();
74 | }
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Alex Peck
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Profiling/Profiling.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net472;net48;net5.0;
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Profiling/Program.cs:
--------------------------------------------------------------------------------
1 | using Base64;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace Profiling
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | Console.WriteLine("Options:");
15 | Console.WriteLine("1. ToUtf8Base64String");
16 | Console.WriteLine("2. FromUtf8Base64String");
17 | Console.WriteLine("3. FromUtf8Base64String (LOH)");
18 | Console.WriteLine("4. CompareFromUtf8Base64String");
19 |
20 | var input = Console.ReadLine();
21 |
22 | if (!int.TryParse(input, out var selectedOption))
23 | {
24 | throw new Exception("not a valid int");
25 | }
26 |
27 | Console.WriteLine("Running...");
28 |
29 | switch (selectedOption)
30 | {
31 | case 1:
32 | ToUtf8Base64String();
33 | break;
34 | case 2:
35 | FromUtf8Base64String();
36 | break;
37 | case 3:
38 | LohTest();
39 | break;
40 | case 4:
41 | CompareFromUtf8Base64String();
42 | break;
43 | default:
44 | throw new Exception("invalid option");
45 | }
46 |
47 | Console.WriteLine("Done");
48 | Console.ReadLine();
49 | }
50 |
51 | private static void ToUtf8Base64String()
52 | {
53 | var s = new string('a', 2048);
54 |
55 | for (int i = 0; i < 640000; i++)
56 | {
57 | s.ToUtf8Base64String();
58 | }
59 | }
60 |
61 | private static void FromUtf8Base64String()
62 | {
63 | var s = new string('a', 8000).ToUtf8Base64String();
64 |
65 | //var resultBytes = Convert.FromBase64String(s);
66 | //var convertString = Encoding.UTF8.GetString(resultBytes);
67 |
68 | for (int i = 0; i < 640000; i++)
69 | {
70 | s.FromUtf8Base64String();
71 | }
72 | }
73 |
74 | private static void LohTest()
75 | {
76 | string lohString = new string('a', 500000).ToUtf8Base64String();
77 |
78 | for (int i = 0; i < 640000; i++)
79 | {
80 | lohString.FromUtf8Base64String();
81 | }
82 |
83 | }
84 |
85 | private static void CompareFromUtf8Base64String()
86 | {
87 | char[] alphabet = ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz".ToUpper() + "0123456789").ToCharArray();
88 |
89 | StringBuilder sb = new StringBuilder(128 * 128);
90 |
91 | for (int i = 0; i < 128; i++)
92 | {
93 | for (int j = 0; j < 128; j++)
94 | {
95 | sb.Append(alphabet[(i + j) % alphabet.Length]);
96 |
97 | var s2 = sb.ToString().ToUtf8Base64String();
98 |
99 | var resultBytes = Convert.FromBase64String(s2);
100 | var convertString = Encoding.UTF8.GetString(resultBytes);
101 |
102 | var xStr = s2.FromUtf8Base64String();
103 |
104 | if (xStr != convertString)
105 | {
106 | Console.WriteLine($"conv: {convertString}");
107 | Console.WriteLine($"xStr: {xStr}");
108 | }
109 | }
110 | }
111 |
112 |
113 | var s = new string('a', 8000).ToUtf8Base64String();
114 |
115 | //var resultBytes = Convert.FromBase64String(s);
116 | //var convertString = Encoding.UTF8.GetString(resultBytes);
117 |
118 | for (int i = 0; i < 640000; i++)
119 | {
120 | s.FromUtf8Base64String();
121 | }
122 | }
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Base64
2 |
3 | Extension methods to enable conversion to and from Base64 strings with reduced memory allocation. Strings can be converted in place, or written/read from streams.
4 |
5 | Intended for use in legacy .NET framework (specifically 4.6).
6 |
7 | ## Conversion via string extension methods
8 |
9 | ```cs
10 | "foo".ToUtf8Base64String().FromUtf8Base64String() == "foo"
11 | ```
12 |
13 | which is equivalent to
14 |
15 | ```cs
16 | var b = Encoding.UTF8.GetBytes("foo");
17 | var s = Convert.ToBase64String(b);
18 | var r = Convert.FromBase64String(s);
19 | "foo" == Encoding.UTF8.GetString(r);
20 | ```
21 |
22 | Internally, these extension methods use RecyclableMemoryStream and pooled buffers to avoid all intermediate byte[] and char[] allocations. For short strings, it falls back to Convert APIs which are faster and optimized for the small case.
23 |
24 |
25 | ## Stream equivalents
26 |
27 | Since stream memory can be recycled, by keeping data in streams and not materializing string instances (e.g. write the stream to blob without ever converting to a string), we can effectively eliminate allocs.
28 |
29 | Read directly from a stream as Base64 string (data can be written to the stream via any stream APIs). Thus, client code can avoid intermediate byte[] allocs:
30 |
31 | ```cs
32 | using (var s = new MemoryStream())
33 | {
34 | s.WriteUtf8AndSetStart("foo"); // write some data to the stream
35 | string base64Str = s.ReadToBase64();
36 | }
37 | ```
38 |
39 | which is equivalent to
40 |
41 | ```cs
42 | byte[] byteArray = Encoding.UTF8.GetBytes("foo");
43 | string base64Str = Convert.ToBase64String(byteArray);
44 | ```
45 |
46 | The reverse is also possible, WriteFromBase64 will write decoded bits directly to a stream, stripping the base64 encoding:
47 |
48 | ```cs
49 | string someBase64EncodedString = "Zm9v";
50 |
51 | using (var s = new MemoryStream())
52 | {
53 | s.WriteFromBase64(someBase64EncodedString);
54 | byte[] decodedBytes = s.ToArray();
55 | }
56 | ```
57 |
58 | which is equivalent to
59 |
60 | ```cs
61 | byte[] decodedBytes = Convert.FromBase64String(someBase64EncodedString);
62 | ```
63 |
64 | ## TODO:
65 |
66 | - Fully optimized FromUtf8Base64String, based on char* end to end. Profiler shows that 20% time spent on
67 | ASCII string => bytes, 20% on bytes => ASCII char. Can just convert to char* from string instantly, save 40%.
68 |
69 |
70 | ## References
71 |
72 | - https://tools.ietf.org/html/rfc4648
73 | - https://commons.apache.org/proper/commons-codec/xref-test/org/apache/commons/codec/binary/Base64Test.html
74 |
75 | ### Using SIMD instructions
76 |
77 | - https://github.com/aklomp/base64
78 | - https://devblogs.microsoft.com/dotnet/the-jit-finally-proposed-jit-and-simd-are-getting-married/
79 | - https://docs.microsoft.com/en-us/dotnet/api/system.runtime.intrinsics.x86?view=netcore-3.1
80 |
81 | System.Numerics doesn't support bit shift operations on Vector. Need to be on .NET Core with the intrinsics APIs to try this.
82 |
83 | ## Benches
84 |
85 | ### StreamToBase64
86 |
87 | | Method | input | Mean | Error | StdDev | Median | Ratio | Gen 0 | Allocated |
88 | |---------- |------ |-------------:|------------:|------------:|-------------:|------:|--------:|----------:|
89 | | ConvertTo | 256 | 950.0 ns | 17.72 ns | 16.58 ns | 944.7 ns | 1.00 | 0.2384 | 1003 B |
90 | | StreamTo | 256 | 584.3 ns | 11.70 ns | 16.02 ns | 578.5 ns | 0.62 | 0.1717 | 722 B |
91 | | | | | | | | | | |
92 | | ConvertTo | 512 | 1,886.4 ns | 37.55 ns | 36.88 ns | 1,865.8 ns | 1.00 | 0.4616 | 1942 B |
93 | | StreamTo | 512 | 1,111.2 ns | 14.75 ns | 13.08 ns | 1,106.7 ns | 0.59 | 0.3338 | 1404 B |
94 | | | | | | | | | | |
95 | | ConvertTo | 1024 | 3,609.6 ns | 36.22 ns | 30.24 ns | 3,606.8 ns | 1.00 | 0.9117 | 3828 B |
96 | | StreamTo | 1024 | 2,237.3 ns | 44.48 ns | 75.52 ns | 2,248.6 ns | 0.61 | 0.6599 | 2778 B |
97 | | | | | | | | | | |
98 | | ConvertTo | 2048 | 7,036.0 ns | 34.34 ns | 32.12 ns | 7,035.2 ns | 1.00 | 1.8082 | 7595 B |
99 | | StreamTo | 2048 | 4,214.0 ns | 46.12 ns | 40.89 ns | 4,209.8 ns | 0.60 | 1.3123 | 5519 B |
100 | | | | | | | | | | |
101 | | ConvertTo | 4096 | 14,342.8 ns | 282.10 ns | 376.59 ns | 14,131.3 ns | 1.00 | 3.5858 | 15127 B |
102 | | StreamTo | 4096 | 8,209.6 ns | 95.10 ns | 84.30 ns | 8,184.6 ns | 0.57 | 2.6093 | 10984 B |
103 | | | | | | | | | | |
104 | | ConvertTo | 8192 | 28,121.2 ns | 550.64 ns | 565.46 ns | 27,831.6 ns | 1.00 | 7.1411 | 30144 B |
105 | | StreamTo | 8192 | 16,720.5 ns | 329.76 ns | 493.58 ns | 16,554.1 ns | 0.60 | 5.1880 | 21904 B |
106 | | | | | | | | | | |
107 | | ConvertTo | 16384 | 54,929.6 ns | 369.26 ns | 308.35 ns | 54,981.0 ns | 1.00 | 14.2822 | 60184 B |
108 | | StreamTo | 16384 | 33,705.6 ns | 658.32 ns | 583.58 ns | 33,909.3 ns | 0.61 | 10.3760 | 43752 B |
109 | | | | | | | | | | |
110 | | ConvertTo | 32768 | 117,409.5 ns | 2,308.33 ns | 2,834.84 ns | 115,435.3 ns | 1.00 | 27.7100 | 120232 B |
111 | | StreamTo | 32768 | 67,350.4 ns | 853.04 ns | 712.33 ns | 67,137.2 ns | 0.58 | 27.7100 | 87416 B |
112 |
113 |
114 |
115 | ### StreamFromBase64
116 |
117 | | Method | input | Mean | Ratio | Allocated |
118 | |------------ |------- |-----------:|------:|----------:|
119 | | ConvertFrom | 256 | 1.482 us | 1.00 | 797 B |
120 | | StreamFrom | 256 | 3.474 us | 2.36 | 264 B |
121 | | | | | | |
122 | | ConvertFrom | 512 | 2.796 us | 1.00 | 1566 B |
123 | | StreamFrom | 512 | 5.296 us | 1.89 | 264 B |
124 | | | | | | |
125 | | ConvertFrom | 1024 | 5.276 us | 1.00 | 3105 B |
126 | | StreamFrom | 1024 | 8.522 us | 1.61 | 265 B |
127 | | | | | | |
128 | | ConvertFrom | 2048 | 10.992 us | 1.00 | 6183 B |
129 | | StreamFrom | 2048 | 15.547 us | 1.46 | 265 B |
130 | | | | | | |
131 | | ConvertFrom | 4096 | 20.937 us | 1.00 | 12340 B |
132 | | StreamFrom | 4096 | 31.543 us | 1.53 | 413 B |
133 | | | | | | |
134 | | ConvertFrom | 8192 | 42.058 us | 1.00 | 24628 B |
135 | | StreamFrom | 8192 | 63.658 us | 1.52 | 561 B |
136 | | | | | | |
137 | | ConvertFrom | 16384 | 83.405 us | 1.00 | 49204 B |
138 | | StreamFrom | 16384 | 124.832 us | 1.50 | 1006 B |
139 | | | | | | |
140 | | ConvertFrom | 32768 | 163.363 us | 1.00 | 98356 B |
141 | | StreamFrom | 32768 | 241.901 us | 1.42 | 1748 B |
142 | | | | | | |
143 | | ConvertFrom | 65536 | 380.923 us | 1.00 | 196648 B |
144 | | StreamFrom | 65536 | 503.032 us | 1.32 | 3384 B |
145 | | | | | | |
146 | | ConvertFrom | 131072 | 772.906 us | 1.00 | 393248 B |
147 | | StreamFrom | 131072 | 965.072 us | 1.25 | 6496 B |
148 |
149 |
150 | ### StringToBase64
151 |
152 |
153 | | Method | data | Mean | Error | StdDev | Ratio | Gen 0 | Allocated |
154 | |---------- |------- |-------------:|------------:|-------------:|------:|---------:|----------:|
155 | | ConvertTo | 1024 | 3,697.6 ns | 35.39 ns | 33.10 ns | 1.00 | 0.9079 | 3828 B |
156 | | StreamTo | 1024 | 4,104.2 ns | 44.44 ns | 34.70 ns | 1.11 | 0.7858 | 3298 B |
157 | | | | | | | | | |
158 | | ConvertTo | 131072 | 457,084.6 ns | 6,405.96 ns | 5,992.14 ns | 1.00 | 142.5781 | 480656 B |
159 | | StreamTo | 131072 | 418,116.2 ns | 8,025.71 ns | 12,729.62 ns | 0.92 | 99.6094 | 350615 B |
160 | | | | | | | | | |
161 | | ConvertTo | 16384 | 60,829.2 ns | 1,229.04 ns | 3,585.17 ns | 1.00 | 14.2822 | 60184 B |
162 | | StreamTo | 16384 | 55,634.1 ns | 1,392.20 ns | 4,061.12 ns | 0.92 | 10.4980 | 44312 B |
163 | | | | | | | | | |
164 | | ConvertTo | 2048 | 7,719.2 ns | 152.65 ns | 335.08 ns | 1.00 | 1.8082 | 7595 B |
165 | | StreamTo | 2048 | 7,392.0 ns | 145.82 ns | 297.87 ns | 0.96 | 1.4343 | 6034 B |
166 | | | | | | | | | |
167 | | ConvertTo | 256 | 978.3 ns | 19.58 ns | 28.70 ns | 1.00 | 0.2384 | 1003 B |
168 | | StreamTo | 256 | 1,917.8 ns | 38.41 ns | 74.01 ns | 1.96 | 0.2956 | 1244 B |
169 | | | | | | | | | |
170 | | ConvertTo | 32768 | 120,500.4 ns | 2,284.94 ns | 2,539.70 ns | 1.00 | 27.7100 | 120232 B |
171 | | StreamTo | 32768 | 100,625.3 ns | 1,979.01 ns | 3,081.08 ns | 0.84 | 27.7100 | 88099 B |
172 | | | | | | | | | |
173 | | ConvertTo | 4096 | 15,362.9 ns | 305.06 ns | 759.70 ns | 1.00 | 3.5706 | 15127 B |
174 | | StreamTo | 4096 | 15,423.5 ns | 456.46 ns | 1,345.89 ns | 1.01 | 2.7466 | 11524 B |
175 | | | | | | | | | |
176 | | ConvertTo | 512 | 1,973.4 ns | 40.11 ns | 117.65 ns | 1.00 | 0.4616 | 1942 B |
177 | | StreamTo | 512 | 2,741.3 ns | 54.74 ns | 150.78 ns | 1.39 | 0.4578 | 1926 B |
178 | | | | | | | | | |
179 | | ConvertTo | 65536 | 238,210.8 ns | 4,699.23 ns | 6,432.36 ns | 1.00 | 55.4199 | 240384 B |
180 | | StreamTo | 65536 | 193,640.5 ns | 1,622.06 ns | 1,437.92 ns | 0.81 | 55.4199 | 175704 B |
181 | | | | | | | | | |
182 | | ConvertTo | 8192 | 28,244.5 ns | 110.96 ns | 92.65 ns | 1.00 | 7.1411 | 30144 B |
183 | | StreamTo | 8192 | 25,210.0 ns | 419.09 ns | 349.96 ns | 0.89 | 5.3406 | 22430 B |
184 |
185 |
186 | ### StringFromBase64
187 |
188 |
189 | | Method | data | Mean | Error | StdDev | Median | Ratio | Gen 0 | Allocated |
190 | |------------ |------- |-------------:|-----------:|-----------:|-------------:|------:|---------:|----------:|
191 | | ConvertFrom | 256 | 1.431 us | 0.0240 us | 0.0225 us | 1.422 us | 1.00 | 0.1888 | 797 B |
192 | | StreamFrom | 256 | 1.434 us | 0.0121 us | 0.0095 us | 1.437 us | 1.00 | 0.1888 | 797 B |
193 | | | | | | | | | | |
194 | | ConvertFrom | 512 | 2.653 us | 0.0411 us | 0.0343 us | 2.649 us | 1.00 | 0.3700 | 1566 B |
195 | | StreamFrom | 512 | 2.750 us | 0.0538 us | 0.0736 us | 2.762 us | 1.04 | 0.3700 | 1566 B |
196 | | | | | | | | | | |
197 | | ConvertFrom | 1024 | 5.182 us | 0.0357 us | 0.0298 us | 5.191 us | 1.00 | 0.7401 | 3105 B |
198 | | StreamFrom | 1024 | 12.637 us | 0.2507 us | 0.3346 us | 12.434 us | 2.46 | 0.6104 | 2564 B |
199 | | | | | | | | | | |
200 | | ConvertFrom | 2048 | 10.099 us | 0.0789 us | 0.0616 us | 10.093 us | 1.00 | 1.4648 | 6183 B |
201 | | StreamFrom | 2048 | 21.218 us | 0.4204 us | 0.5004 us | 21.106 us | 2.09 | 1.0986 | 4619 B |
202 | | | | | | | | | | |
203 | | ConvertFrom | 4096 | 20.243 us | 0.3419 us | 0.2855 us | 20.129 us | 1.00 | 2.9297 | 12340 B |
204 | | StreamFrom | 4096 | 39.915 us | 0.7939 us | 1.1637 us | 39.909 us | 1.97 | 2.0752 | 8878 B |
205 | | | | | | | | | | |
206 | | ConvertFrom | 8192 | 40.005 us | 0.6649 us | 0.5552 us | 39.743 us | 1.00 | 5.8594 | 24628 B |
207 | | StreamFrom | 8192 | 73.411 us | 0.6841 us | 0.6399 us | 73.551 us | 1.83 | 4.0283 | 17218 B |
208 | | | | | | | | | | |
209 | | ConvertFrom | 16384 | 79.310 us | 0.7240 us | 0.6046 us | 79.353 us | 1.00 | 11.5967 | 49204 B |
210 | | StreamFrom | 16384 | 145.456 us | 2.9057 us | 2.9839 us | 144.455 us | 1.84 | 8.0566 | 34052 B |
211 | | | | | | | | | | |
212 | | ConvertFrom | 32768 | 157.254 us | 0.9824 us | 0.7670 us | 157.071 us | 1.00 | 23.1934 | 98356 B |
213 | | StreamFrom | 32768 | 282.910 us | 3.5762 us | 2.7921 us | 282.367 us | 1.80 | 15.6250 | 67566 B |
214 | | | | | | | | | | |
215 | | ConvertFrom | 65536 | 322.867 us | 1.0735 us | 0.8964 us | 323.180 us | 1.00 | 41.5039 | 196648 B |
216 | | StreamFrom | 65536 | 580.539 us | 11.4230 us | 14.0285 us | 576.197 us | 1.79 | 41.0156 | 134848 B |
217 | | | | | | | | | | |
218 | | ConvertFrom | 131072 | 727.617 us | 6.1183 us | 5.7231 us | 727.427 us | 1.00 | 116.2109 | 393248 B |
219 | | StreamFrom | 131072 | 1,161.373 us | 20.9996 us | 19.6431 us | 1,165.704 us | 1.60 | 80.0781 | 269048 B |
220 |
221 | This is comparing to crypto stream, memory allocs are excessive and perf tanks:
222 |
223 | | Method | data | Mean | Error | StdDev | Median | Ratio | Gen 0 | Allocated |
224 | |----------------- |------- |--------------:|--------------:|--------------:|--------------:|------:|----------:|----------:|
225 | | ConvertFrom | 1024 | 5.212 us | 0.0772 us | 0.0685 us | 5.192 us | 1.00 | 0.7401 | 3105 B |
226 | | StreamFrom | 1024 | 14.102 us | 0.2803 us | 0.4526 us | 13.966 us | 2.73 | 0.6104 | 2564 B |
227 | | CryptoStreamFrom | 1024 | 340.905 us | 6.6863 us | 7.1542 us | 337.914 us | 65.36 | 15.1367 | 64173 B |
228 | | | | | | | | | | |
229 | | ConvertFrom | 131072 | 724.461 us | 14.1122 us | 16.2516 us | 728.312 us | 1.00 | 116.2109 | 393248 B |
230 | | StreamFrom | 131072 | 1,333.259 us | 25.6557 us | 34.2496 us | 1,342.586 us | 1.84 | 78.1250 | 269204 B |
231 | | CryptoStreamFrom | 131072 | 48,468.583 us | 2,781.3085 us | 8,113.2075 us | 48,924.591 us | 62.92 | 1937.5000 | 8138814 B |
232 |
--------------------------------------------------------------------------------