Skip to content

Instantly share code, notes, and snippets.

@cannorin
Created January 6, 2016 08:08
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 cannorin/8e342f7cccd926463080 to your computer and use it in GitHub Desktop.
Save cannorin/8e342f7cccd926463080 to your computer and use it in GitHub Desktop.
Base4225
open System
open System.IO
open System.Text
open System.Collections.Generic
open System.Linq
let base64 = List.concat[['A'..'Z']; ['a'..'z']; ['0'..'9']; ['+'; '/'; '=']]
let b2u b1 b2 =
let i1 = List.findIndex ((=) b1) base64 in
let i2 = List.findIndex ((=) b2) base64 in
let offset = i2 + (i1 * 65) in
let a = offset / (64 * 64) in
let b = (offset - (a * 64 * 64)) / 64 in
let c = (offset - (a * 64 * 64) - (b * 64)) in
let x = [0xf0; 0xa0 + a; 0x80 + b; 0x80 + c] |> List.map (byte) |> Seq.toArray |> Encoding.UTF8.GetString in
//printfn "%A" x; Console.ReadLine() |> ignore;
x
let u2b (s : string) =
let bs = Encoding.UTF8.GetBytes(s) in
if BitConverter.ToInt32(bs |> Seq.rev |> Seq.toArray, 0) < 0xf0a08080 then
""
else
let i = ((int)bs.[1] - 0xa0) * 64 * 64
+ ((int)bs.[2] - 0x80) * 64
+ ((int)bs.[3] - 0x80) in
let a = i / 65 in
let b = i - a * 65 in
let x = [base64.[a]; base64.[b]] |> String.Concat in
//printfn "%s" x; Console.ReadLine() |> ignore;
x
let f2k f =
let bs = File.ReadAllBytes (f) in
let b64 = Convert.ToBase64String (bs) |> Seq.toArray in
let ln = b64.Length / 2 in
List.map (fun i -> b2u b64.[i * 2 - 2] b64.[i * 2 - 1]) [1..ln] |> String.Concat
let k2f (s : string) =
let ln = s.Length / 2 in
let b64 = List.map (fun i -> String([|s.[i * 2 - 2]; s.[i * 2 - 1]|]) |> u2b) [1..ln] |> String.Concat in
Convert.FromBase64String (b64)
[<EntryPoint>]
let main argv =
let d = argv.Contains "-d" || argv.Contains "--decode" in
let argv = Seq.filter (fun x -> not (x.Equals "-d" || x.Equals "--decode")) argv |> Seq.toArray
if argv.Length > 0 then
let fn = argv.[0] in
try
if File.Exists fn then
if d then
let r = File.ReadAllBytes(fn) in
let bs = Encoding.UTF8.GetString(r) |> k2f in
use stdout = Console.OpenStandardOutput(bs.Length) in
stdout.Write(bs, 0, bs.Length)
0
else
let s = f2k fn in
printf "%s" s
0
else
printfn "%s" ("base4225: file " + fn + " not found")
1
with
| e ->
printfn "%s" "base4225: fatal error: "
e.ToString () |> printfn "%s"
1
else
printfn "%s" "usage: base4225 [OPTION]... [FILE]"
printfn "%s" "Base4225 encode or decode FILE to standard output."
Console.WriteLine ()
printfn "%s" "OPTIONS:"
printfn "%s" " -d, --decode ... decode data"
0
𠒅𠥫𠎑𠦆𠂢𠀊𠀏𠘘𠀀𠀘𠇱𠘑𠾀𠶧𠗶𠐳𠅏𠠯𠂝𠪮𠒸𠵂𠄇𠂣𠻛𠭥𠐉𠰑𠺯𠆫𠡅𠰾𠃐𠋃𠪘𡀾𠷍𠽳𠇛𠜣𠃬𠒎𠓀𠄤𠄪𠣥𠶩𠥗𠒎𠌣𠂋𠒱𠞆𠐚𠂀𠹩𠆉𠌌𠀎𠤙𠳱𠠎𠸢𠽣𠇒𠫇𠡒𠡽𠓻𠌔𠄧𠺫𠣷𠡞𠋚𡀾𡀾𠐷𠆦𠭗𠌁𠟆𠯙𠀡𠇄𡀕𠮲𠩽𠔀𠴙𠷋𠭕𠈑𠞙𠛴𠊳𠭓𠅢𠖃𠼺𡀼𠠝𠹼𠉓𠕈𠎸𡀕𠹚𠆠𠷠𠖘𠐟𠄦𠹭𠊒𠘽𠈷𠣨𠦨𠙵𠣆𠲞𠡪𠼥𠹇𠹪𠈎𠕳𠒮𠗥𠰌𠤊𠺈𠽤𠆛𠾲𠝘𠄟𠄈𠜹𠇼𠩃𠈬𠞱𠬮𠭧𠘅𡀽𠾊𠐶𠅹𠛅𠾹𠘇𠺢𠡂𠇁𠕉𠈧𡀾𠽏𠍳𠚅𠴲𡀺𠣒𠣪𠷀𠣉𠭽𠏭𠀪𠇃𠂦𠟢𠈦𠈠𠒂𠗫𠉒𠔬𠜳𠆢𠀠𠆗𠩀𠄛𠣻𠲥𠪊𠡅𠤿𠆻𠾁𠋢𠀩𠌞𠵮𠉍𠣇𠻙𠽥𠄒𠬏𠫃𠊛𠣰𠴲𡀻𠃙𠩑𠑵𠕊𠹐𠇙𠔰𠈒𠒗𠙤𠈏𠂦𠗏𡀱𠑗𠜨𠇾𠾼𠽷𠉓𠮑𠎀𠜶𠆩𠞭𠢄𠢖𠜋𠡵𠞌𡀾𠻉𠈆𠹽𠉤𠜓𠳸𠮄𠶅𠋄𠤿𠆞𠳽𠶛𠚝𠢅𠌟𠆲𠤔𠰳𠕅𠐨𠴙𠿯𠄤𠊜𠰰𠀀𠀀𠂢𠀊𠀀𠀇𠒉𠀡𠡂𠄴𠟖𠎕𠙋𠀣𠢒𠪒𠐀𠞮𠟐𠹩𠃌𠀾𠔪𠂒𠐣𠹖𠎵𠕒𠐦𠂤𠘙𠈳𠋝𠆩𠀺𠊌𠏅𠡞𠎱𠹶𠘜𠆈𠧺𠩟𠐓𠹧𠢒𠉬𠈙𠒢𠢙𠎧𠋳𠎏𠀃𠭬
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment