Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Created August 18, 2013 07:57
Show Gist options
  • Save tmplinshi/6260481 to your computer and use it in GitHub Desktop.
Save tmplinshi/6260481 to your computer and use it in GitHub Desktop.
; ======================================================================================================================
; Namespace: Base64
; AHK version: AHK 1.1.+ (required)
; Function: Methods for Base64 en/decoding.
; Language: English
; Tested on: Win XPSP3, Win VistaSP2 (U32) / Win 7 (U64)
; Version: 1.0.00.00/2012-08-12/just me
; URL: http://msdn.microsoft.com/en-us/library/windows/desktop/aa380252(v=vs.85).aspx
; Remarks: The ANSI functions are used by default to save space. ANSI needs about 133 % of the original size for
; encoding, whereas Unicode needs about 266 %.
; ======================================================================================================================
; This software is provided 'as-is', without any express or implied warranty.
; In no event will the authors be held liable for any damages arising from the use of this software.
; ======================================================================================================================
Class Base64 {
; CRYPT_STRING_BASE64 = 0x01
; ===================================================================================================================
; Base64.Encode()
; Parameters:
; VarIn - Variable containing the input buffer
; SizeIn - Size of the input buffer in bytes
; VarOut - Variable to receive the encoded buffer
; Use - Use ANSI (A) or Unicode (W) function
; Default: A
; Return values:
; On success: Number of bytes copied to VarOut, not including the terminating null character
; On failure: False
; Remarks:
; VarIn may contain any binary contents including NUll bytes.
; ===================================================================================================================
Encode(ByRef VarIn, ByRef VarOut, Use = "A") {
Static Codec := 0x01 ; CRYPT_STRING_BASE64
SizeIn := StrLen(VarIn) << !!A_IsUnicode ; +++++++++++
If (Use <> "W")
Use := "A"
Enc := "Crypt32.dll\CryptBinaryToString" . Use
If !DllCall(Enc, "Ptr", &VarIn, "UInt", SizeIn, "UInt", Codec, "Ptr", 0, "UIntP", SizeOut)
Return False
VarSetCapacity(VarOut, SizeOut << (Use = "W"), 0)
If !DllCall(Enc, "Ptr", &VarIn, "UInt", SizeIn, "UInt", Codec, "Ptr", &VarOut, "UIntP", SizeOut)
Return False
Return SizeOut << (Use = "W")
}
; ===================================================================================================================
; Base64.Decode()
; Parameters:
; VarIn - Variable containing a null-terminated Base64 encoded string
; VarOut - Variable to receive the decoded buffer
; Use - Use ANSI (A) or Unicode (W) function
; Default: A
; Return values:
; On success: Number of bytes copied to VarOut
; On failure: False
; Remarks:
; VarOut may contain any binary contents including NUll bytes.
; ===================================================================================================================
Decode(ByRef VarIn, ByRef VarOut, Use = "A") {
Static Codec := 0x01 ; CRYPT_STRING_BASE64
If (Use <> "W")
Use := "A"
Dec := "Crypt32.dll\CryptStringToBinary" . Use
If !DllCall(Dec, "Ptr", &VarIn, "UInt", 0, "UInt", Codec, "Ptr", 0, "UIntP", SizeOut, "Ptr", 0, "Ptr", 0)
Return False
VarSetCapacity(VarOut, SizeOut, 0)
If !DllCall(Dec, "Ptr", &VarIn, "UInt", 0, "UInt", Codec, "Ptr", &VarOut, "UIntP", SizeOut, "Ptr", 0, "Ptr", 0)
Return False
VarSetCapacity(VarOut, -1) ; +++++++++++
Return SizeOut
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment