Public Function IsGoodBase64Dec(Optional numArrays As Long) As Boolean
' verify correct Base64Dec, returns True if all tests are passed
' checks provided by Guido Beckmann, G.Beckmann@NikoCity.de, 20011204
' update 20021020
    
    Const CHARSETBASE64$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
                           "abcdefghijklmnopqrstuvwxyz" & _
                           "0123456789+/"
    
    Dim sSrc As String, sTrg As String
    Dim abSrc() As Byte, abTrg() As Byte
    Dim abDecode() As Byte, c&, d&, i&, sBase64$
    
    ReDim abDecode(0 To 2)
    sBase64 = "****"

    i = 0:        GoSub Test
    i = &H11111:  GoSub Test
    i = &H33333:  GoSub Test
    i = &H77777:  GoSub Test
    i = &HBBBBB:  GoSub Test
    i = &H222222: GoSub Test
    i = &H444444: GoSub Test
    i = &H888888: GoSub Test
    i = &HAAAAAA: GoSub Test
    i = &HFFEEDD: GoSub Test

    For c = 4 To 100
        sBase64 = String(((c + 2) \ 3) * 4, "A")
        i = c Mod 3
        If i Then Mid$(sBase64, Len(sBase64) - (2 - i)) = "=="
        
        GoSub Decode
        
        If UBound(abTrg()) <> (c - 1) Then Stop: Exit Function
    Next c

    IsGoodBase64Dec = True
    Exit Function

Test:
    For c = i To i + 1000
        abDecode(0) = c \ &H10000
        abDecode(1) = c \ &H100 And &HFF
        abDecode(2) = c And &HFF

        i = c
        For d = 0 To 3
            Mid$(sBase64, 4 - d, 1) = Mid$(CHARSETBASE64, (i Mod 64) + 1, 1)
            i = i \ 64
        Next d

        GoSub Decode
        
        If UBound(abTrg()) <> UBound(abDecode()) Then Stop: Exit Function
        For d = 0 To UBound(abTrg())
            If abTrg(d) <> abDecode(d) Then Stop: Exit Function
        Next d
    Next c
    Return
Decode:
    'replace "Base64Dec02" with the name of your function to test
    Select Case numArrays
    Case 0
      ' string in, string out
      sTrg = Base64Dec01(sBase64)
      abTrg = StrConv(sTrg, vbFromUnicode)
    Case 1
      ' string in, array out
      Call Base64Dec02(sBase64, abTrg())
      sTrg = Base64Enc03(abTrg())
    Case 2
      ' array in, array out
      abSrc = StrConv(sBase64, vbFromUnicode)
      Base64Dec03 abSrc(), abTrg()
      sTrg = StrConv(abTrg(), vbUnicode)
    End Select
    Return
End Function

Back to Base64Dec