|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.Management.Automation; |
6 | 7 | using System.Text; |
7 | 8 |
|
8 | 9 | namespace Microsoft.PowerShell.TextUtility |
9 | 10 | { |
10 | | - [Cmdlet(VerbsData.ConvertFrom, "Base64")] |
| 11 | + [Cmdlet(VerbsData.ConvertFrom, "Base64", DefaultParameterSetName="Text")] |
11 | 12 | [OutputType(typeof(string))] |
12 | 13 | public sealed class ConvertFromBase64Command : PSCmdlet |
13 | 14 | { |
14 | 15 | /// <summary> |
15 | 16 | /// Gets or sets the base64 encoded string. |
16 | 17 | /// </summary> |
17 | | - [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true)] |
| 18 | + [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")] |
18 | 19 | public string EncodedText { get; set; } |
19 | 20 |
|
| 21 | + /// <summary> |
| 22 | + /// Gets or sets the AsByteArray switch. |
| 23 | + /// </summary> |
| 24 | + [Parameter()] |
| 25 | + public SwitchParameter AsByteArray { get; set; } |
| 26 | + |
20 | 27 | protected override void ProcessRecord() |
21 | 28 | { |
22 | 29 | var base64Bytes = Convert.FromBase64String(EncodedText); |
23 | | - WriteObject(Encoding.UTF8.GetString(base64Bytes)); |
| 30 | + |
| 31 | + if (AsByteArray) |
| 32 | + { |
| 33 | + WriteObject(base64Bytes); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + WriteObject(Encoding.UTF8.GetString(base64Bytes)); |
| 38 | + } |
24 | 39 | } |
25 | 40 | } |
26 | 41 |
|
27 | | - [Cmdlet(VerbsData.ConvertTo, "Base64")] |
| 42 | + [Cmdlet(VerbsData.ConvertTo, "Base64", DefaultParameterSetName="Text")] |
28 | 43 | [OutputType(typeof(string))] |
29 | 44 | public sealed class ConvertToBase64Command : PSCmdlet |
30 | 45 | { |
31 | 46 | /// <summary> |
32 | 47 | /// Gets or sets the text to encoded to base64. |
33 | 48 | /// </summary> |
34 | | - [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true)] |
| 49 | + [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")] |
35 | 50 | public string Text { get; set; } |
36 | 51 |
|
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the base64 encoded byte array. |
| 54 | + /// </summary> |
| 55 | + [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="ByteArray")] |
| 56 | + public byte[] ByteArray { get; set; } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets or sets the InsertBreakLines switch. |
| 60 | + /// </summary> |
| 61 | + [Parameter()] |
| 62 | + public SwitchParameter InsertBreakLines { get; set; } |
| 63 | + |
| 64 | + private List<byte> _bytearray = new List<byte>(); |
| 65 | + private Base64FormattingOptions _base64Option = Base64FormattingOptions.None; |
| 66 | + |
37 | 67 | protected override void ProcessRecord() |
38 | 68 | { |
39 | | - var textBytes = Encoding.UTF8.GetBytes(Text); |
40 | | - WriteObject(Convert.ToBase64String(textBytes)); |
| 69 | + if (InsertBreakLines) |
| 70 | + { |
| 71 | + _base64Option = Base64FormattingOptions.InsertLineBreaks; |
| 72 | + } |
| 73 | + |
| 74 | + if (ParameterSetName.Equals("Text")) |
| 75 | + { |
| 76 | + var textBytes = Encoding.UTF8.GetBytes(Text); |
| 77 | + WriteObject(Convert.ToBase64String(textBytes, _base64Option)); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + _bytearray.AddRange(ByteArray); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + protected override void EndProcessing() |
| 86 | + { |
| 87 | + if (ParameterSetName.Equals("ByteArray")) |
| 88 | + { |
| 89 | + WriteObject(Convert.ToBase64String(_bytearray.ToArray(), _base64Option)); |
| 90 | + } |
41 | 91 | } |
42 | 92 | } |
43 | 93 | } |
0 commit comments