'
' Manual implementation of Base64 decoder.
'
' CAUTION: This implementation MAY BE EXTREMELY SLOW & INEFFICIENT on big inputs!
'
Public Function obf_DecodeBaseText64(ByVal obf_sString As String) As String
    On Error GoTo obf_ProcError
    Dim obf_bOut() As Byte, obf_bIn() As Byte, obf_bTrans(255) As Byte, obf_lPowers6(63) As Long, obf_lPowers12(63) As Long
    Dim obf_lPowers18(63) As Long, obf_lQuad As Long, obf_iPad As Integer, obf_lChar As Long, obf_lPos As Long, obf_sOut As String
    Dim obf_lTemp As Long
    Dim obf_Counter

    obf_sString = Replace(obf_sString, vbCr, vbNullString)
    obf_sString = Replace(obf_sString, vbLf, vbNullString)

    obf_lTemp = Len(obf_sString) Mod 4
    If obf_lTemp Then
        obf_DecodeBaseText64 = #""#
    End If

    If InStrRev(obf_sString, $"=="$) Then
        obf_iPad = 2
    ElseIf InStrRev(obf_sString, $"="$) Then
        obf_iPad = 1
    End If

    For obf_lTemp = 0 To 255
        Select Case obf_lTemp
            Case 65 To 90
                obf_bTrans(obf_lTemp) = obf_lTemp - 65
            Case 97 To 122
                obf_bTrans(obf_lTemp) = obf_lTemp - 71
            Case 48 To 57
                obf_bTrans(obf_lTemp) = obf_lTemp + 4
            Case 43
                obf_bTrans(obf_lTemp) = 62
            Case 47
                obf_bTrans(obf_lTemp) = 63
        End Select
    Next obf_lTemp

    For obf_lTemp = 0 To 63
        obf_lPowers6(obf_lTemp) = obf_lTemp * 64
        obf_lPowers12(obf_lTemp) = obf_lTemp * 4096
        obf_lPowers18(obf_lTemp) = obf_lTemp * 262144
    Next obf_lTemp

    obf_bIn = StrConv(obf_sString, vbFromUnicode)
    ReDim obf_bOut((((UBound(obf_bIn) + 1) \ 4) * 3) - 1)

    For obf_lChar = 0 To UBound(obf_bIn) Step 4
        obf_lQuad = obf_lPowers18(obf_bTrans(obf_bIn(obf_lChar))) + obf_lPowers12(obf_bTrans(obf_bIn(obf_lChar + 1))) + _
                obf_lPowers6(obf_bTrans(obf_bIn(obf_lChar + 2))) + obf_bTrans(obf_bIn(obf_lChar + 3))
        obf_lTemp = obf_lQuad And 16711680
        obf_bOut(obf_lPos) = obf_lTemp \ 65536
        obf_lTemp = obf_lQuad And 65280
        obf_bOut(obf_lPos + 1) = obf_lTemp \ 256
        obf_bOut(obf_lPos + 2) = obf_lQuad And 255
        obf_lPos = obf_lPos + 3
    Next obf_lChar

    ' This for-loop adjusts each byte by adding <<<SHIFTKEY>>> to evade AVs capable of
    ' base64-decoding in-the-fly
    For obf_Counter = LBound(obf_bOut) To UBound(obf_bOut)
        obf_bOut(obf_Counter) = (obf_bOut(obf_Counter) + <<<SHIFTKEY>>>) Mod 256
    Next

    obf_sOut = StrConv(obf_bOut, vbUnicode)
    If obf_iPad Then obf_sOut = Left$(obf_sOut, Len(obf_sOut) - obf_iPad)

    obf_DecodeBaseText64 = obf_sOut
    Exit Function

obf_ProcError:
End Function

Private Function obf_DecodeBase64(ByVal obf_EncodedData As String) As Byte()
    On Error GoTo obf_ProcError
    Dim obf_temp As String
    obf_temp = obf_DecodeBaseText64(obf_EncodedData)
    obf_DecodeBase64 = StrConv(obf_temp, vbFromUnicode)
obf_ProcError:
End Function
