Added VBScript support.

This commit is contained in:
James Forshaw
2017-05-16 19:00:29 +01:00
parent e3cebf073f
commit b53cd212a0
8 changed files with 214 additions and 29 deletions
+7
View File
@@ -54,6 +54,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="VBAGenerator.cs" />
<Compile Include="VBScriptGenerator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
@@ -76,6 +77,12 @@
<ItemGroup>
<None Include="Resources\jscript_auto_version_script.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\vbs_template.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\vbs_auto_version_script.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
+10 -4
View File
@@ -65,16 +65,22 @@ namespace DotNetToJScript
}
}
public string GenerateScript(byte[] serialized_object, string entry_class_name, string additional_script, RuntimeVersion version, bool enable_debug)
public static string[] BinToBase64Lines(byte[] serialized_object)
{
if ((serialized_object.Length % 3) != 0)
int ofs = serialized_object.Length % 3;
if (ofs != 0)
{
int length = serialized_object.Length + (3 - serialized_object.Length % 3);
int length = serialized_object.Length + (3 - ofs);
Array.Resize(ref serialized_object, length);
}
string base64 = Convert.ToBase64String(serialized_object, Base64FormattingOptions.InsertLineBreaks);
string[] lines = base64.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Select(s => String.Format("\"{0}\"", s)).ToArray();
return base64.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Select(s => String.Format("\"{0}\"", s)).ToArray();
}
public string GenerateScript(byte[] serialized_object, string entry_class_name, string additional_script, RuntimeVersion version, bool enable_debug)
{
string[] lines = BinToBase64Lines(serialized_object);
return GetScriptHeader(version, enable_debug)
+ Properties.Resources.jscript_template.Replace("%SERIALIZED%", String.Join("+"+Environment.NewLine, lines)).Replace("%CLASS%", entry_class_name).Replace("%ADDEDSCRIPT%", additional_script);
+4
View File
@@ -35,6 +35,7 @@ namespace DotNetToJScript
{
JScript,
VBA,
VBScript,
}
static object BuildLoaderDelegate(byte[] assembly)
@@ -196,6 +197,9 @@ namespace DotNetToJScript
case ScriptLanguage.VBA:
generator = new VBAGenerator();
break;
case ScriptLanguage.VBScript:
generator = new VBScriptGenerator();
break;
default:
throw new ArgumentException("Invalid script language option");
}
+65 -25
View File
@@ -61,16 +61,14 @@ namespace DotNetToJScript.Properties {
}
/// <summary>
/// Looks up a localized string similar to function setversion() {
/// var shell = new ActiveXObject(&apos;WScript.Shell&apos;);
/// ver = &apos;v4.0.30319&apos;;
/// try {
/// shell.RegRead(&apos;HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\&apos;);
/// } catch(e) {
/// ver = &apos;v2.0.50727&apos;;
/// }
/// shell.Environment(&apos;Process&apos;)(&apos;COMPLUS_Version&apos;) = ver;
/// Looks up a localized string similar to var shell = new ActiveXObject(&apos;WScript.Shell&apos;);
///ver = &apos;v4.0.30319&apos;;
///try {
///shell.RegRead(&apos;HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\&apos;);
///} catch(e) {
///ver = &apos;v2.0.50727&apos;;
///}
///shell.Environment(&apos;Process&apos;)(&apos;COMPLUS_Version&apos;) = ver;
///.
/// </summary>
internal static string jscript_auto_version_script {
@@ -80,25 +78,23 @@ namespace DotNetToJScript.Properties {
}
/// <summary>
/// Looks up a localized string similar to var serialized_obj = [
///%SERIALIZED%
///];
/// Looks up a localized string similar to function base64ToStream(b) {
/// var enc = new ActiveXObject(&quot;System.Text.ASCIIEncoding&quot;);
/// var length = enc.GetByteCount_2(b);
/// var ba = enc.GetBytes_4(b);
/// var transform = new ActiveXObject(&quot;System.Security.Cryptography.FromBase64Transform&quot;);
/// ba = transform.TransformFinalBlock(ba, 0, length);
/// var ms = new ActiveXObject(&quot;System.IO.MemoryStream&quot;);
/// ms.Write(ba, 0, (length / 4) * 3);
/// ms.Position = 0;
/// return ms;
///}
///
///var serialized_obj = %SERIALIZED%;
///var entry_class = &apos;%CLASS%&apos;;
///
///try {
/// setversion();
/// var stm = new ActiveXObject(&apos;System.IO.MemoryStream&apos;);
/// var fmt = new ActiveXObject(&apos;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&apos;);
/// var al = new ActiveXObject(&apos;System.Collections.ArrayList&apos;)
///
/// for (i in serialized_obj) {
/// stm.WriteByte(serialized_obj[i]);
/// }
///
/// stm.Position = 0;
/// var n = fmt.SurrogateSelector;
/// var d = fmt.Deserialize_2(stm);
/// a [rest of string was truncated]&quot;;.
/// setver [rest of string was truncated]&quot;;.
/// </summary>
internal static string jscript_template {
get {
@@ -151,5 +147,49 @@ namespace DotNetToJScript.Properties {
return ResourceManager.GetString("vba_template", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Dim shell
///Set shell = CreateObject(&apos;WScript.Shell&apos;)
///
///Dim ver
///ver = &quot;v4.0.30319&quot;;
///
///On Error Resume Next
///shell.RegRead &quot;HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\&quot;
///
///ver = &apos;v2.0.50727&apos;;
///}
///shell.Environment(&apos;Process&apos;)(&apos;COMPLUS_Version&apos;) = ver;
///.
/// </summary>
internal static string vbs_auto_version_script {
get {
return ResourceManager.GetString("vbs_auto_version_script", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Function Base64ToStream(b)
/// Dim enc, length, ba, transform, ms
/// Set enc = CreateObject(&quot;System.Text.ASCIIEncoding&quot;)
/// length = enc.GetByteCount_2(b)
/// Set transform = CreateObject(&quot;System.Security.Cryptography.FromBase64Transform&quot;)
/// Set ms = CreateObject(&quot;System.IO.MemoryStream&quot;)
/// ms.Write transform.TransformFinalBlock(enc.GetBytes_4(b), 0, length), 0, ((length / 4) * 3)
/// ms.Position = 0
/// Set Base64ToStream = ms
///End Function
///
///Sub Run
///Dim s, entry_class
///s = %SERIALIZED%
///entry_class = &quot;%CL [rest of string was truncated]&quot;;.
/// </summary>
internal static string vbs_template {
get {
return ResourceManager.GetString("vbs_template", resourceCulture);
}
}
}
}
@@ -130,4 +130,10 @@
<data name="vba_template" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\vba_template.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="vbs_auto_version_script" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\vbs_auto_version_script.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="vbs_template" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\vbs_template.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>
@@ -0,0 +1,9 @@
Dim ver
ver = "v4.0.30319"
On Error Resume Next
shell.RegRead "HKLM\SOFTWARE\\Microsoft\.NETFramework\v4.0.30319\"
If Err.Number <> 0 Then
ver = "v2.0.50727"
Err.Clear
End If
shell.Environment("Process").Item("COMPLUS_Version") = ver
@@ -0,0 +1,33 @@
Function Base64ToStream(b)
Dim enc, length, ba, transform, ms
Set enc = CreateObject("System.Text.ASCIIEncoding")
length = enc.GetByteCount_2(b)
Set transform = CreateObject("System.Security.Cryptography.FromBase64Transform")
Set ms = CreateObject("System.IO.MemoryStream")
ms.Write transform.TransformFinalBlock(enc.GetBytes_4(b), 0, length), 0, ((length / 4) * 3)
ms.Position = 0
Set Base64ToStream = ms
End Function
Sub Run
Dim s, entry_class
s = %SERIALIZED%
entry_class = "%CLASS%"
Dim fmt, al, d, o
Set fmt = CreateObject("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter")
Set al = CreateObject("System.Collections.ArrayList")
al.Add fmt.SurrogateSelector
Set d = fmt.Deserialize_2(Base64ToStream(s))
Set o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class)
%ADDEDSCRIPT%
End Sub
SetVersion
On Error Resume Next
Run
If Err.Number <> 0 Then
Debug Err.Description
Err.Clear
End If
+80
View File
@@ -0,0 +1,80 @@
// This file is part of DotNetToJScript - A tool to generate a
// JScript which bootstraps an arbitrary v2.NET Assembly and class.
// Copyright (C) James Forshaw 2017
//
// DotNetToJScript is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DotNetToJScript is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with DotNetToJScript. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Text;
namespace DotNetToJScript
{
class VBScriptGenerator : IScriptGenerator
{
static string GetScriptHeader(RuntimeVersion version, bool enable_debug)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("Sub Debug(s)");
if (enable_debug)
{
builder.AppendLine("Wscript.Echo s");
}
builder.AppendLine("End Sub");
builder.AppendLine("Sub SetVersion");
if (version != RuntimeVersion.None)
{
builder.AppendLine("Dim shell");
builder.AppendLine("Set shell = CreateObject(\"WScript.Shell\")");
switch (version)
{
case RuntimeVersion.v2:
builder.AppendLine("shell.Environment(\"Process\").Item(\"COMPLUS_Version\") = \"v2.0.50727\"");
break;
case RuntimeVersion.v4:
builder.AppendLine("shell.Environment(\"Process\").Item(\"COMPLUS_Version\") = \"v4.0.30319\"");
break;
case RuntimeVersion.Auto:
builder.AppendLine(Properties.Resources.vbs_auto_version_script);
break;
}
}
builder.AppendLine("End Sub");
return builder.ToString();
}
public string ScriptName
{
get
{
return "VBScript";
}
}
public bool SupportsScriptlet
{
get
{
return true;
}
}
public string GenerateScript(byte[] serialized_object, string entry_class_name, string additional_script, RuntimeVersion version, bool enable_debug)
{
string[] lines = JScriptGenerator.BinToBase64Lines(serialized_object);
return GetScriptHeader(version, enable_debug) + Properties.Resources.vbs_template.Replace("%SERIALIZED%",
String.Join(Environment.NewLine + "s = s & ", lines)).Replace("%CLASS%", entry_class_name).Replace("%ADDEDSCRIPT%", additional_script);
}
}
}