⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clsb64.cls

📁 快速Base64编码_解码引擎全部源程序
💻 CLS
📖 第 1 页 / 共 3 页
字号:
' | aryIn  - input array                                                        |
' | aryOut - output array (will be redimensioned)                               |
' |                                                                             |
' | Description :                                                               |
' | Size of output is always 1/4 smaller than the original (4 bytes becomes 3). |
' | The reversed mechanisme was used base on the encode mechanisme. Output size |
' | maybe not dividable perfectly by 3, thus a checkup on last 2 bytes of input |
' | is done. If 1 "=" encounted then the last group of 4 bytes is decoded into  |
' | 2 bytes, and if 2 "=" is encounted 1 bytes will be output instead.          |
' +-----------------------------------------------------------------------------+

Public Sub Decode(ByRef aryIn() As Byte, ByRef aryOut() As Byte)

Dim SizeIn As Long      ' Size of input array
Dim SizeOut As Long     ' Size of output array (to be calculate)
Dim SizeMod As Integer  ' Remainder of output
Dim ActSizeIn As Long   ' Actual input (dividable by 4)
Dim Index As Long       ' Decode Input
Dim oIndex As Long      ' Output Index
Dim Buffer(3) As Byte   ' Temporary buffer to store 4 bytes of data to be decoded

SizeIn = UBound(aryIn) + 1      ' Get the size of input
ActSizeIn = SizeIn

If aryIn(SizeIn - 1) = 61 Then          ' Last byte is a "="
    If aryIn(SizeIn - 2) = 61 Then      ' Second Last byte is a"="
        SizeMod = 1                     ' Output remainder should be 1 byte
    Else
        SizeMod = 2                     ' Output remainder should be 2 bytes
    End If
    ActSizeIn = SizeIn - 4              ' Substitude the last group of 4 bytes since they are incomplete (contains "=")
    If ActSizeIn < 0 Then ActSizeIn = 0 ' Since input maybe just "dA==" or "dHA=" less than 4 complete bytes instead of "GfgrDfF="
End If

SizeOut = (ActSizeIn / 4 * 3) + SizeMod ' Output size is 3/4 of input plus the output remainder

ReDim aryOut(SizeOut - 1)               ' Redimension output array

If ActSizeIn >= 4 Then                  ' Have more than 1 group of complete 4 bytes

    For Index = 0 To ActSizeIn - 1 Step 4
    
        Buffer(0) = B64DecTable(aryIn(Index))       ' Input 4 bytes
        Buffer(1) = B64DecTable(aryIn(Index + 1))   ' and decode them
        Buffer(2) = B64DecTable(aryIn(Index + 2))
        Buffer(3) = B64DecTable(aryIn(Index + 3))
        
        aryOut(oIndex + 0) = ((Buffer(0) * 4) Or ((Buffer(1) And &H30) \ 16))
        aryOut(oIndex + 1) = (((Buffer(1) And &HF) * 16) Or ((Buffer(2) And &H3C)) \ 4)
        aryOut(oIndex + 2) = (((Buffer(2) And &H3) * 64) Or Buffer(3))
    
        oIndex = oIndex + 3             ' Increment output index
        
    Next

End If

If SizeMod = 1 Then         ' Output has 1 remainder byte

        Buffer(0) = B64DecTable(aryIn(SizeIn - 4))  ' input 2 remaining bytes from input
        Buffer(1) = B64DecTable(aryIn(SizeIn - 3))  ' this byte has portion of 1st output byte ONLY
        Buffer(2) = 0                               ' ignore the 2 "="
        
        ' output 1 byte
        aryOut(oIndex + 0) = ((Buffer(0) * 4) Or ((Buffer(1) And &H30) \ 16))
        
ElseIf SizeMod = 2 Then     ' Output has 2 remainder byte

        Buffer(0) = B64DecTable(aryIn(SizeIn - 4))  ' input the 3 remaining bytes
        Buffer(1) = B64DecTable(aryIn(SizeIn - 3))
        Buffer(2) = B64DecTable(aryIn(SizeIn - 2))  ' this byte has portion of 2nd output byte ONLY
        Buffer(3) = 0                               ' ignore the last "="
        
        ' output 2 bytes
        aryOut(oIndex + 0) = ((Buffer(0) * 4) Or ((Buffer(1) And &H30) \ 16))
        aryOut(oIndex + 1) = (((Buffer(1) And &HF) * 16) Or ((Buffer(2) And &H3C)) \ 4)

End If

End Sub

' +------------------------------------------------------------------------------+
' | Makes the code looks tidier                                                  |
' +------------------------------------------------------------------------------+
' | Parameter   :                                                                |
' | aryIn         - input byte array                                             |
' | aryOut        - output string array (to be redimensioned)                    |
' | SpanSize      - numbers of character per line (without SpanSeparator)        |
' | SpanSeparator - Separator which will be append to end of line (eg vbCrLf)    |
' |                                                                              |
' | Description :                                                                |
' | This is important when the file has been encoded. Before the encoded data is |
' | attached to email body it must be span into lines, and this sub does it all. |
' +------------------------------------------------------------------------------+
'
Public Sub Span(ByRef aryIn() As Byte, ByRef aryOut() As String, Optional SpanSize As Integer, Optional SpanSeparator As String)

Dim SizeIn As Long      ' Size of input byte array
Dim SizeMod As Integer  ' Remainder (last line which it's size is less than SpanSize)
Dim TotalLines As Long  ' Total lines of string which has the size of SpanSize
Dim sIndex As Long      ' Span Processing Index
Dim wIndex As Long      ' Output Index
Dim lIndex As Long      ' Line Index
Dim Buffer() As Byte    ' Temporary storage for converting string to byte array
Dim tmpStr As String    ' Temporary storage for next line

If SpanSize = 0 Then SpanSize = 76                  ' Loads the default span size

SizeIn = UBound(aryIn) + 1                          ' Get the size of input
SizeMod = SizeIn Mod SpanSize                       ' Get the remainder of input (which cannot form a complete line with size of SpanSize)
TotalLines = (SizeIn - SizeMod) / SpanSize          ' The numbers of lines which can be form with "SpanSize" of characters

If SizeMod > 0 Then TotalLines = TotalLines + 1     ' If remainder is not empty, create another extra line to store the extra line

ReDim Buffer(SpanSize - 1)                          ' Create the temporary processing buffer
ReDim aryOut(TotalLines - 1)                        ' Create the output string array

For sIndex = 0 To SizeIn - SizeMod - 1 Step SpanSize
    
    For wIndex = 0 To SpanSize - 1
        Buffer(wIndex) = aryIn(sIndex + wIndex)     ' Load data from byte array to buffer
    Next
    
    tmpStr = StrConv(Buffer, vbUnicode)             ' Convert them into string
    
    If SpanSeparator <> "" Then tmpStr = tmpStr + SpanSeparator         ' SpanSeparator is applied, append it on the the line end
    
    aryOut(lIndex) = tmpStr                         ' Put into output array
    lIndex = lIndex + 1                             ' Increment output line index

Next

If SizeMod > 0 Then                                 ' Remainder of input
    
    For wIndex = 0 To SizeMod - 1
        Buffer(wIndex) = aryIn((SizeIn - 1) - (SizeMod - 1) + wIndex)   ' Load them into buffer
    Next
    
    tmpStr = StrConv(Buffer, vbUnicode)             ' Again, convert to string
        
    If SpanSeparator <> "" Then tmpStr = tmpStr + SpanSeparator         ' Append SpanSeparator if supplied
    
    aryOut(TotalLines - 1) = tmpStr                 ' Put into output string array

End If

End Sub

' +--------------------------------------------------------------------------------+
' | Restores the original form of encoded data                                     |
' +--------------------------------------------------------------------------------+
' | Parameter   :                                                                  |
' | aryIn         - input string array                                             |
' | aryOut        - output byte array (to be redimensioned)                        |
' | SpanSize      - numbers of character per line (without SpanSeparator)          |
' | SpanSeparator - Separator which will be removed from end of line (eg vbCrLf)   |
' |                                                                                |
' | Description :                                                                  |
' | Used for converting multiple lines of encoded data into byte array (undecoded) |
' | and removes the separator too                                                  |
' +--------------------------------------------------------------------------------+

Public Sub Unspan(ByRef aryIn() As String, ByRef aryOut() As Byte, Optional SpanSize As Integer, Optional SpanSeparator As String)

Dim TotalLines As Long  ' Total lines to be converted
Dim SizeOut As Long     ' Size of output array
Dim SizeMod As Long     ' Size of remainder line
Dim tmpStr As String    ' Temporary storage to fetch next line
Dim Buffer() As Byte    ' Temporary storage to convert string to byte array
Dim sIndex As Long      ' Unspan processing index
Dim wIndex As Long      ' Output index
Dim oIndex As Long      ' Output Line index

TotalLines = UBound(aryIn) + 1                          ' Get size of input
If SpanSize = 0 Then SpanSize = 76                      ' Load default SpanSize

If Len(aryIn(TotalLines - 1)) - Len(SpanSeparator) < SpanSize Then  ' Last line is remainder line or not
    SizeMod = Len(aryIn(TotalLines - 1)) - Len(SpanSeparator)       ' If yes, get the remaining size
    TotalLines = TotalLines - 1                                     ' Decrement total lines, since last line is not a complete line
End If

SizeOut = TotalLines * SpanSize + SizeMod               ' Calculate the output size

ReDim aryOut(SizeOut - 1)                               ' Create the output byte array

If TotalLines >= 1 Then                                 ' More than 1 line (sometimes the 1st line can be remainder line)

    For sIndex = 0 To TotalLines - 1
        
        tmpStr = aryIn(sIndex)                          ' Load into temporary buffer
        
        If SpanSeparator <> "" Then
            tmpStr = Left(tmpStr, InStr(tmpStr, SpanSeparator) - 1) ' Remove the SpanSeparator
        End If
        
        Buffer = StrConv(tmpStr, vbFromUnicode)         ' Convert to byte array
        
        For wIndex = 0 To SpanSize - 1
            aryOut(oIndex + wIndex) = Buffer(wIndex)    ' Load from temporary buffer into output byte array
        Next
        
        oIndex = oIndex + SpanSize                      ' Increment output array index
    
    Next

End If

If SizeMod > 0 Then                                     ' The remainder line
    
    tmpStr = aryIn(TotalLines)                          ' Get the last line (remainder line)
    
    If SpanSeparator <> "" Then
        tmpStr = Left(tmpStr, InStr(tmpStr, SpanSeparator) - 1) ' Remove SpanSeparator
    End If
    
    Buffer = StrConv(tmpStr, vbFromUnicode)             ' Convert to byte array
    
    For wIndex = 0 To SizeMod - 1
        aryOut(oIndex + wIndex) = Buffer(wIndex)        ' Load into output byte array
    Next
    
End If

End Sub

' +---------------------------------+
' | Visual Basic is the best!!! ^_^ |
' +---------------------------------+

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -