Skip to content

Commit bb7632e

Browse files
committed
feat: migrate ConvertFrom-Base64ToByteArray to Rust library
- Replace .NET System.Convert.FromBase64String with Rust base64_to_bytes - Update ConvertFrom-Base64ToMemoryStream test for Rust error messages
1 parent 0b6972b commit bb7632e

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

src/Convert/Public/ConvertFrom-Base64ToByteArray.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Converts a Base 64 Encoded String to a Byte Array
44
55
.DESCRIPTION
6-
Converts a Base 64 Encoded String to a Byte Array
6+
Converts a Base 64 Encoded String to a Byte Array.
77
88
.PARAMETER String
99
The Base 64 Encoded String to be converted
@@ -24,7 +24,7 @@
2424
https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToByteArray/
2525
#>
2626
function ConvertFrom-Base64ToByteArray {
27-
[CmdletBinding()]
27+
[CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToByteArray/')]
2828
[Alias('ConvertFrom-Base64StringToByteArray')]
2929
param
3030
(
@@ -40,14 +40,32 @@ function ConvertFrom-Base64ToByteArray {
4040

4141
begin {
4242
$userErrorActionPreference = $ErrorActionPreference
43+
$nullPtr = [IntPtr]::Zero
4344
}
4445

4546
process {
4647
foreach ($s in $String) {
48+
$ptr = $nullPtr
4749
try {
48-
[System.Convert]::FromBase64String($s)
50+
$length = [UIntPtr]::Zero
51+
$ptr = [ConvertCoreInterop]::base64_to_bytes($s, [ref]$length)
52+
53+
if ($ptr -eq $nullPtr) {
54+
$errorMsg = GetRustError -DefaultMessage "Base64 to byte array conversion failed"
55+
throw $errorMsg
56+
}
57+
58+
$byteArray = New-Object byte[] $length.ToUInt64()
59+
[System.Runtime.InteropServices.Marshal]::Copy($ptr, $byteArray, 0, $byteArray.Length)
60+
61+
# Output the byte array (use comma to prevent PowerShell from unrolling)
62+
,$byteArray
4963
} catch {
5064
Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference
65+
} finally {
66+
if ($ptr -ne $nullPtr) {
67+
[ConvertCoreInterop]::free_bytes($ptr)
68+
}
5169
}
5270
}
5371
}

src/Tests/Unit/ConvertFrom-Base64ToMemoryStream.Tests.ps1

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ Describe -Name $function -Fixture {
3232
It -Name 'Supports EAP Continue' -Test {
3333
$assertion = ConvertFrom-Base64ToMemoryStream -String ([int]1) -ErrorAction Continue 2>&1
3434

35-
$exception = @(
36-
# PowerShell
37-
'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.'
38-
39-
# Windows PowerShell
40-
'Invalid length for a Base-64 char array or string.'
41-
)
42-
$assertion[0].Exception.InnerException.Message | Should -BeIn $exception
35+
$assertion[0].Exception.Message | Should -BeLike 'Failed to decode Base64:*'
4336
}
4437
}
4538
}

0 commit comments

Comments
 (0)