📄 module2.bas
字号:
Attribute VB_Name = "Module2"
'Module 代码一
Option Explicit
'============================= Base64
Private m_bytIndex(0 To 63) As Byte
Private m_bytReverseIndex(0 To 255) As Byte
Private Const k_bytMask1 As Byte = &H3 '3 '00000011
Private Const k_bytMask2 As Byte = &HF '15 '00001111
Private Const k_bytMask3 As Byte = &H3F '63 '00111111
Private Const k_bytMask4 As Byte = &HC0 '192 '11000000
Private Const k_bytMask5 As Byte = &HF0 '240 '11110000
Private Const k_bytMask6 As Byte = &HFC '252 '11111100
Private Const k_bytShift2 As Byte = &H4 '4
Private Const k_bytShift4 As Byte = &H10 '16
Private Const k_bytShift6 As Byte = &H40 '64
Private Const k_lMaxBytesPerLine As Long = 152 '76 character
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Public Function DecodeArr(ByVal sInput As String) As Byte()
'returns a SBCS byte array
Dim bytInput() As Byte 'base64 encoded string to work with
Dim bytWorkspace() As Byte 'byte array to use as workspace
Dim bytResult() As Byte 'array that result will be copied to
Dim lInputCounter As Long 'iteration counter for input array
Dim lWorkspaceCounter As Long 'iteration counter for workspace array
'get rid of CrLfs, and "="s and spaces since they're not required for decoding,
Dim strTmp As String
Dim strArr() As String
Dim i As Long
strArr() = Split(sInput, vbCrLf)
For i = 0 To UBound(strArr())
strTmp = strTmp & Trim$(strArr(i))
Next
For i = 0 To 1
If Right$(strTmp, 1) = "=" Then
strTmp = Left$(strTmp, Len(strTmp) - 1)
End If
Next i
'place the input in the byte array
bytInput = strTmp
'size the workspace
ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput) * 2)) As Byte
lWorkspaceCounter = LBound(bytWorkspace)
'pass bytes back through index to get original values
For lInputCounter = LBound(bytInput) To UBound(bytInput)
bytInput(lInputCounter) = m_bytReverseIndex(bytInput(lInputCounter))
Next lInputCounter
For lInputCounter = LBound(bytInput) To (UBound(bytInput) - ((UBound(bytInput) Mod 8) + 8)) Step 8
'left shift first input byte by 2 and right shift second input byte by 4
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4)
'mask bits 5-8 of second byte, left shift it by 4
'right shift third byte by 2, add it to result of second byte
bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytMask2) * k_bytShift4) + _
(bytInput(lInputCounter + 4) \ k_bytShift2)
'mask bits 3-8 of third byte, left shift it by 6, add it to fourth byte
bytWorkspace(lWorkspaceCounter + 2) = ((bytInput(lInputCounter + 4) And k_bytMask1) * k_bytShift6) + _
bytInput(lInputCounter + 6)
lWorkspaceCounter = lWorkspaceCounter + 3
Next lInputCounter
'decode any remaining bytes that are not part of a full 4 byte block
Select Case (UBound(bytInput) Mod 8):
Case 3:
'left shift first input byte by 2 and right shift second input byte by 4
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4)
Case 5:
'left shift first input byte by 2 and right shift second input byte by 4
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4)
'mask bits 5-8 of second byte, left shift it by 4
'right shift third byte by 2, add it to result of second byte
bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytMask2) * k_bytShift4) + _
(bytInput(lInputCounter + 4) \ k_bytShift2)
lWorkspaceCounter = lWorkspaceCounter + 1
Case 7:
'left shift first input byte by 2 and right shift second input byte by 4
bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4)
'mask bits 5-8 of second byte, left shift it by 4
'right shift third byte by 2, add it to result of second byte
bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytMask2) * k_bytShift4) + _
(bytInput(lInputCounter + 4) \ k_bytShift2)
'mask bits 3-8 of third byte, left shift it by 6, add it to fourth byte
bytWorkspace(lWorkspaceCounter + 2) = ((bytInput(lInputCounter + 4) And k_bytMask1) * k_bytShift6) + _
bytInput(lInputCounter + 6)
lWorkspaceCounter = lWorkspaceCounter + 2
End Select
'size the result array
ReDim bytResult(LBound(bytWorkspace) To lWorkspaceCounter) As Byte
'if option base is set to 1 then don't increment this value
If LBound(bytWorkspace) = 0 Then
lWorkspaceCounter = lWorkspaceCounter + 1
End If
'move decoded data to a properly sized array
CopyMemory VarPtr(bytResult(LBound(bytResult))), VarPtr(bytWorkspace(LBound(bytWorkspace))), lWorkspaceCounter
'return
DecodeArr = bytResult
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -