-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBase64.ahk
72 lines (67 loc) · 2.59 KB
/
Base64.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Library: Base64
* Functions for Base64 (en|de)coding
* Version:
* v1.0.00.00
* Requirements:
* AutoHotkey v1.1.20.00+ OR v2.0-a+ (latest preferred)
* Windows XP or higher
* Installation:
* Use #Include or copy to a function library folder
* Remarks:
* Based on similar lib(s) found in the forum.
*/
/* Function: Base64_Encode
* Encode data to Base64
* Syntax:
* bytes := Base64_Encode( ByRef data, len, ByRef out [ , mode := "A" ] )
* Parameter(s):
* bytes [retval] - on success, the number of bytes copied to 'out'.
* data [in, ByRef] - data to encode
* len [in] - size in bytes of 'data'. Specify '-1' to automtically
* calculate the size.
* out [out, ByRef] - out variable containing the Base64 encoded data
* mode [in, opt] - Specify 'A'(default) to use the ANSI version of
* 'CryptBinaryToString'. Otherwise, 'W' for UNICODE.
*/
Base64_Encode(ByRef data, len:=-1, ByRef out:="", mode:="A")
{
if !InStr("AW", mode := Format("{:U}", mode), true)
mode := "A"
BytesPerChar := mode=="W" ? 2 : 1
if (Round(len) <= 0)
len := StrLen(data) * (A_IsUnicode ? 2 : 1)
; CRYPT_STRING_BASE64 := 0x00000001
if DllCall("Crypt32\CryptBinaryToString" . mode, "Ptr", &data, "UInt", len
, "UInt", 0x00000001, "Ptr", 0, "UIntP", size)
{
VarSetCapacity(out, size *= BytesPerChar, 0)
if DllCall("Crypt32\CryptBinaryToString" . mode, "Ptr", &data, "UInt", len
, "UInt", 0x00000001, "Ptr", &out, "UIntP", size)
return size * BytesPerChar
}
}
/* Function: Base64_Decode
* Decode Base64 encoded data
* Syntax:
* bytes := Base64_Decode( ByRef data, ByRef out [ , mode := "A" ] )
* Parameter(s):
* bytes [retval] - on success, the number of bytes copied to 'out'.
* data [in, ByRef] - data(NULL-terminated) to decode
* out [out, ByRef] - out variable containing the decoded data
* mode [in, opt] - Specify 'A'(default) to use the ANSI version of
* 'CryptStringToBinary'. Otherwise, 'W' for UNICODE.
*/
Base64_Decode(ByRef data, ByRef out, mode:="A")
{
if !InStr("AW", mode := Format("{:U}", mode), true)
mode := "A"
; CRYPT_STRING_BASE64 := 0x00000001
if DllCall("Crypt32\CryptStringToBinary" . mode, "Ptr", &data, "UInt", 0
, "UInt", 0x00000001, "Ptr", 0, "UIntP", size, "Ptr", 0, "Ptr", 0)
{
VarSetCapacity(out, size, 0)
if DllCall("Crypt32\CryptStringToBinary" . mode, "Ptr", &data, "UInt", 0
, "UInt", 0x00000001, "Ptr", &out, "UIntP", size, "Ptr", 0, "Ptr", 0)
return size
}
}