Skip to content

Instantly share code, notes, and snippets.

@HumanEquivalentUnit
Created August 24, 2020 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HumanEquivalentUnit/b088c971d4fe7cd8c24641368dc7a981 to your computer and use it in GitHub Desktop.
Save HumanEquivalentUnit/b088c971d4fe7cd8c24641368dc7a981 to your computer and use it in GitHub Desktop.
Decode APLCart TSV and show in Out-Gridview (C# decoder)
# Imports the table.tsv file from APLCart
# Decodes the TIO.run links
# Shows in Out-Gridview
$tablePath = "c:\sc\aplcart\table.tsv"
$csharp = @'
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class TIOAssist
{
public static string decodeTIO(string TIOLink)
{
// remove domain parts from URL
string hash = (new Uri(TIOLink)).Fragment.Trim('#');
// convert hash to base64, pad length to multipe of 4 with =, decode to bytes
string b64 = WebUtility.UrlDecode(hash).Replace('@', '+').Replace('-', '+').Replace('_', '/');
byte[] deflatedBytes = Convert.FromBase64String(b64 + (new string('=', ((4 - (b64.Length % 4)) % 4))));
// inflate() (decompress) bytes
MemoryStream inflatedStream = new System.IO.MemoryStream();
(new System.IO.Compression.DeflateStream(
(new MemoryStream(deflatedBytes)),
System.IO.Compression.CompressionMode.Decompress
)).CopyTo(inflatedStream);
Regex rIndent = new System.Text.RegularExpressions.Regex("^", RegexOptions.Compiled | RegexOptions.Multiline);
List<string> fields = new List<string>();
byte[] bytes = inflatedStream.ToArray();
int last = 0;
int i = 0;
while (i < bytes.Length) {
if (bytes[i] == (byte)0xff) {
fields.Add(Encoding.UTF8.GetString(bytes, last, i-last));
last = i+1;
}
i++;
}
fields.Add(Encoding.UTF8.GetString(bytes, last, bytes.Length - last));
List<string> result = new List<string>();
if ("" != fields[1]) { result.Add("HEADER:\n" + rIndent.Replace(fields[1], " ")); }
if ("" != fields[2]) { result.Add("CODE:\n" + rIndent.Replace(fields[2], " ")); }
if ("" != fields[3]) { result.Add("FOOTER:\n" + rIndent.Replace(fields[3], " ")); }
if ("" != fields[4]) { result.Add("INPUT:\n" + rIndent.Replace(fields[4], " ")); }
return String.Join("\n", result);
}
}
'@
Add-Type -TypeDefinition $csharp
Import-Csv -Path $tablePath -Delimiter "`t" |
Select-Object -Property Syntax, TIO, @{Label="TIOCode";Expression={[TIOAssist]::decodeTIO($_.TIO)}} |
Out-GridView -OutputMode Multiple |
Format-List -Property *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment