Skip to content

Commit

Permalink
Fill with initial codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
VALDINGER committed Jan 9, 2019
0 parents commit 6897382
Show file tree
Hide file tree
Showing 30 changed files with 4,378 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Base64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Management.Automation;
using System;

namespace Base64
{
[Cmdlet(VerbsData.ConvertTo, "Base64String")]
[OutputType(typeof(string))]
public class ConvertToBase64Command : Cmdlet
{
// Declare the parameters for the cmdlet.
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
[Alias("InputString","String","Text")]
public string InputObject { get; set; }


[Parameter]
[ArgumentCompleter(typeof(EncodingCompleter))]
[EncodingTransform]
public System.Text.Encoding Encoding { get; set; } = EncodingTransformAttribute.StandardEncoding["UTF8"];


//This is the "Process {}" block of a Powershell script
protected override void ProcessRecord()
{
//WriteObject("Hello " + name + "!");

byte[] bytes = Encoding.GetBytes(InputObject);
WriteObject(Convert.ToBase64String(bytes), true);

}
} //class

[Cmdlet(VerbsData.ConvertFrom, "Base64String")]
[OutputType(typeof(string))]
public class ConvertFromBase64Command : Cmdlet
{
// Declare the parameters for the cmdlet.
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
[Alias("InputString","String","Text")]
public string InputObject { get; set; }


[Parameter]
[ArgumentCompleter(typeof(EncodingCompleter))]
[EncodingTransform]
public System.Text.Encoding Encoding { get; set; } = EncodingTransformAttribute.StandardEncoding["UTF8"];


//This is the "Process {}" block of a Powershell script
protected override void ProcessRecord()
{
//WriteObject("Hello " + name + "!");
// $Encoding.GetString([System.Convert]::FromBase64String($Base64String))
byte[] bytes = Convert.FromBase64String(InputObject);
WriteObject(Encoding.GetString(bytes), true);
}
} //class
} //namespace
11 changes: 11 additions & 0 deletions Base64.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
</ItemGroup>

</Project>
87 changes: 87 additions & 0 deletions EncodingTransformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;

namespace Base64
{
class EncodingTransformAttribute : ArgumentTransformationAttribute
{
public EncodingTransformAttribute() { }
internal static Dictionary<string, Encoding> StandardEncoding = new Dictionary<string, Encoding>()
{
{ "ASCII", Encoding.ASCII },
{ "UnicodeBE", Encoding.BigEndianUnicode },
{ "Unicode", Encoding.Unicode },
{ "UTF32", Encoding.UTF32 },
{ "UTF7", Encoding.UTF7 },
{ "UTF8", new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) },
{ "UTF8BOM", Encoding.UTF8 }
};

public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
switch (inputData)
{
case Encoding e:
return e;
case string s:
if (StandardEncoding.ContainsKey(s))
{
return StandardEncoding[s];
}
else
{
return Encoding.GetEncoding(s);
}
default:
throw new ArgumentTransformationMetadataException();
}
}

public override string ToString()
{
return "[EncodingTransformAttribute]";
}
}

class EncodingCompleter : IArgumentCompleter
{
public EncodingCompleter() { }
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
List<string> encodingValues = new List<string>(EncodingTransformAttribute.StandardEncoding.Keys);

if (!string.IsNullOrEmpty(wordToComplete))
{
foreach (string encoding in encodingValues)
{
yield return new CompletionResult(
completionText: encoding,
listItemText: encoding,
resultType: CompletionResultType.ParameterValue,
toolTip: string.Format("{0} {1}", "[Encoding]", encoding));
}
}
else
{
var filteredList = encodingValues.FindAll(x => x.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase));
foreach (string result in filteredList)
{
yield return new CompletionResult(
completionText: result,
listItemText: result,
resultType: CompletionResultType.ParameterValue,
toolTip: string.Format("{0} {1}", "[Encoding]", result));
}
}
}
}
}
Loading

0 comments on commit 6897382

Please sign in to comment.