Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Base64enc() / Base64dec() - Base64 encoder / decoder


  • Please log in to reply
4 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Required OS -> XP or greater.
Versions by Polyethene / Laszlo are already available in forum.
I have just re-wrapped the following pair to support unicode AHK.

[color=#FF0000]Base64enc([/color] ByRef OutData, ByRef InData, InDataLen [color=#FF0000])[/color] { ; by SKAN
 DllCall( "Crypt32.dll\CryptBinaryToString" ( A_IsUnicode ? "W" : "A" )
        , UInt,&InData, UInt,InDataLen, UInt,1, UInt,0, UIntP,TChars, "CDECL Int" )
 VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ) )
 DllCall( "Crypt32.dll\CryptBinaryToString" ( A_IsUnicode ? "W" : "A" )
        , UInt,&InData, UInt,InDataLen, UInt,1, Str,OutData, UIntP,Req, "CDECL Int" )
Return TChars
}

[color=#FF0000]Base64dec([/color] ByRef OutData, ByRef InData [color=#FF0000])[/color] { ; by SKAN
 DllCall( "Crypt32.dll\CryptStringToBinary" ( A_IsUnicode ? "W" : "A" ), UInt,&InData
        , UInt,StrLen(InData), UInt,1, UInt,0, UIntP,Bytes, Int,0, Int,0, "CDECL Int" )
 VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ) )
 DllCall( "Crypt32.dll\CryptStringToBinary" ( A_IsUnicode ? "W" : "A" ), UInt,&InData
        , UInt,StrLen(InData), UInt,1, Str,OutData, UIntP,Req, Int,0, Int,0, "CDECL Int" )
Return Bytes
}
[color=#FFFFFF]; Encoding / Decoding[/color]


Example for DECODE ( Try this first! This script creates ahk.png used by the other example )

SetWorkingDir, %A_ScriptDir%
GoSub, LoadPNGDATA
Bytes := Base64Dec( BIN, PNGDATA )
VarZ_Save( BIN, Bytes, "ahk.png" )
VarSetcapacity( PNGDATA, 0 )

Gui, Margin, 20, 20
Gui, Add, Picture,, ahk.png
Gui, Show
Return                                                 ; // end of auto-execute section //


GuiClose:
 ExitApp

Base64dec( ByRef OutData, ByRef InData ) {
 DllCall( "Crypt32.dll\CryptStringToBinary" ( A_IsUnicode ? "W" : "A" ), UInt,&InData
        , UInt,StrLen(InData), UInt,1, UInt,0, UIntP,Bytes, Int,0, Int,0, "CDECL Int" )
 VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ) )
 DllCall( "Crypt32.dll\CryptStringToBinary" ( A_IsUnicode ? "W" : "A" ), UInt,&InData
        , UInt,StrLen(InData), UInt,1, Str,OutData, UIntP,Req, Int,0, Int,0, "CDECL Int" )
Return Bytes
}

VarZ_Save( ByRef Data, DataSize, TrgFile ) { ; By SKAN
[color=#008040]; http://www.autohotkey.com/community/viewtopic.php?t=45559[/color]
 hFile :=  DllCall( "_lcreat", ( A_IsUnicode ? "AStr" : "Str" ),TrgFile, UInt,0 )
 IfLess, hFile, 1, Return "", ErrorLevel := 1
 nBytes := DllCall( "_lwrite", UInt,hFile, UInt,&Data, UInt,DataSize, UInt )
 DllCall( "_lclose", UInt,hFile )
Return nBytes
}

LoadPNGDATA:
PNGData=
(
[color=#808080]iVBORw0KGgoAAAANSUhEUgAAAPAAAABOCAYAAAAelZuXAAAMvElEQVR42uydUW8U
1xmG9yJyHCuBNKItILBQFVUVErkIVxG+yHXpbS9K/0DoDyDkB6T8AJL2HgtxgRCh
LkKkQsRyXAKinV27jrGQ2a4Xs+suZrMYh7q5mO6z8ddwfHb3eM5skWfOZ+nFO3hm
PPP4POd8c2bWLmzjY6id4W1n39GRnRyOMUkcbJSf8tt5/Ka++tvB6b9+9V6tvjKz
vLwcVyqVThYflHvm/v37WU7Xc6pWq3G9Xp/58tad96bulA4qP+W34/kV/zF/9FGt
FlUfLtdXnzRj0mq14tbTtf9lbS3r4Tx6h3OWVKsP48XFB/VHtXo0f+/eu8pP+e1Y
fp9//pfRperD4vKjWtx4vNpJs9k04g+SbV5WvI7DOtd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MJvNiHKS179iOp06VxAeQoj150JkOmsek/CnDiP8cQI7QSxWIpAAUYkhkH4J
wYV46IgvmsjjoUaLw8nzZDKxgwge7mL8xSHBmqx5TN5f9h3G+5MTWG6kI5CVpBYk
kYTgD5oXpLcIKFzyLOo6hQ536k/9LcuffQJf4sd+v3+IQCAamFSGgfTjSY9AChRv
R/+4v3/AgGoKHQJB/am/pfmTW0nM9l6pvKjy9jMECmyYANQmUcj+HSLWT/K4pfny
vVar7+Wu3rxBoUOgIP4Gg8EP9af+kvBH/7darVn+wtN6eltb2wUj+XA0Gp0IZ2Vk
PB4vAvb1r8wlTZp3vV7vYGd3954EYAT1p/6W4o/FHhFCLWiGBD7rdrtHDO3ikh0H
V+zz8ulzJGfuS/5UWwbfbYbDoQPbnhN7e2q+n+12+0u9Xn/Kqx2ZQQJnNra/Tqfz
1fRVfqk/9TcnfyfG3zfXX3gJooGX84VC4XG5XH7v+x+OGX4FzWbTodFoJM2p+/V9
34bf5k6r1ZLPx6VS6V2xWHwUTOe5FrhSf+ovPf6s2tAz3KJhbXho4KyvGF4aXgW8
zuUuvDmd3Ns0wzFGQf6CvD43PDE84CED5NF0kb6b+lN/qfInfWGaMqxIRxmRwT2n
24Y7hrsBmxb3V5zNEOTv4hXyirQNIn6BizVAHqg/9Zc6fyLRGmLoAX0TaoA/yAPT
e2QFCowF0q6BNFss1J/6S6c/FjuwAPRNQpCol7+eWbgCgOQ/LE/9qb80+3NrQ9rd
QGJWopnCzluEOPWn/lbHn9SGDkjNIm5eZVF/6i+V/n4D8vgs7OivT6kAAAAASUVO
RK5CYII=[/color]
)
Return


Example for ENCODE

SetWorkingDir, %A_ScriptDir%

File := "ahk.png"
FileGetSize, Size, %File%
FileRead, Bin, *c %File%
Base64enc( PNGDATA, Bin, Size )
MsgBox % Clipboard := PNGDATA

Return                                                 ; // end of auto-execute section //

Base64enc( ByRef OutData, ByRef InData, InDataLen ) {
 DllCall( "Crypt32.dll\CryptBinaryToString" ( A_IsUnicode ? "W" : "A" )
        , UInt,&InData, UInt,InDataLen, UInt,1, UInt,0, UIntP,TChars, "CDECL Int" )
 VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ) )
 DllCall( "Crypt32.dll\CryptBinaryToString" ( A_IsUnicode ? "W" : "A" )
        , UInt,&InData, UInt,InDataLen, UInt,1, Str,OutData, UIntP,Req, "CDECL Int" )
Return TChars
}


kWo4Lk1.png

just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
THX SKAN! Thought about the same thingy some weeks ago.

:arrow: AHK_L Class version

Prefer ahkscript.org for the time being.


gamax92
  • Members
  • 411 posts
  • Last active: Aug 06 2013 05:00 AM
  • Joined: 05 Dec 2010

I know that this is a bit of time to bump this post, but I'm having a big issue with this library.

 

It appears that when I do a decode request on the same variable used before, I'll get feed back chars.

 

For example, the first decode sets VarX to:

Hello World, isn't it a grand day?

Then a shorter string gives me this:

It sure is, lovely.it a grand day?

 

Is this library not ment to be used for decoding to text?

I know that I can simply just use: VarX := "", or VarSetCapacity(VarX, 0) but its still odd that im getting this garbage out.



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

 VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ), 0 )

+++

 VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ), 0 )

 

 


Prefer ahkscript.org for the time being.


gamax92
  • Members
  • 411 posts
  • Last active: Aug 06 2013 05:00 AM
  • Joined: 05 Dec 2010

Thats nice, explain why im getting trailing garbage on my output variable.