Public Function Encode(strToEncode As String, Optional srcIsFile As Boolean, Optional writeFile As Boolean, Optional outFileName) As String
Dim a As Long, b As Integer, c As Byte, d As Byte, decVal As Byte, z As Long, y As Long 'counters
Dim fso, ff As Integer, newFileName As String 'file vars
Dim padCount As Byte 'determines count of equal signs at the end of file
Dim charInput() As Byte 'holds input data
Dim mimeArray(111111) As Byte 'holds the mime equiv from index(binary) to value(ascii)
Dim addArray(11) As Long 'holds values from 100000 - 1 in binary
Dim dblConvArray(255, 2, 3) As Double 'holds every character and the possible positions 1,2,3
Dim charOutput() As Byte 'final output
'We have to make sure people use common sense!
If Not Len(strToEncode) > 0 Then Exit Function
If srcIsFile = False And writeFile And IsMissing(outFileName) Then
Err.Raise 10001, "Encoder DLL - Encode", "You must include the output file name when the source is not a file!"
Exit Function
End If
If srcIsFile Then
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(strToEncode) Then
Err.Raise 10002, "Encoder.DLL - Encode", "The path specified is invalid!"
Exit Function
End If
End If
'Make the mime array
mimeArray(0) = 65: mimeArray(1) = 66: mimeArray(10) = 67:
mimeArray(11) = 68: mimeArray(100) = 69: mimeArray(101) = 70:
mimeArray(110) = 71: mimeArray(111) = 72: mimeArray(1000) = 73:
mimeArray(1001) = 74: mimeArray(1010) = 75: mimeArray(1011) = 76:
mimeArray(1100) = 77: mimeArray(1101) = 78: mimeArray(1110) = 79:
mimeArray(1111) = 80: mimeArray(10000) = 81: mimeArray(10001) = 82:
mimeArray(10010) = 83: mimeArray(10011) = 84: mimeArray(10100) = 85:
mimeArray(10101) = 86: mimeArray(10110) = 87: mimeArray(10111) = 88:
mimeArray(11000) = 89: mimeArray(11001) = 90: mimeArray(11010) = 97:
mimeArray(11011) = 98: mimeArray(11100) = 99: mimeArray(11101) = 100:
mimeArray(11110) = 101: mimeArray(11111) = 102: mimeArray(100000) = 103:
mimeArray(100001) = 104: mimeArray(100010) = 105: mimeArray(100011) = 106:
mimeArray(100100) = 107: mimeArray(100101) = 108: mimeArray(100110) = 109:
mimeArray(100111) = 110: mimeArray(101000) = 111: mimeArray(101001) = 112:
mimeArray(101010) = 113: mimeArray(101011) = 114: mimeArray(101100) = 115:
mimeArray(101101) = 116: mimeArray(101110) = 117: mimeArray(101111) = 118:
mimeArray(110000) = 119: mimeArray(110001) = 120: mimeArray(110010) = 121:
mimeArray(110011) = 122: mimeArray(110100) = 48: mimeArray(110101) = 49:
mimeArray(110110) = 50: mimeArray(110111) = 51: mimeArray(111000) = 52:
mimeArray(111001) = 53: mimeArray(111010) = 54: mimeArray(111011) = 55:
mimeArray(111100) = 56: mimeArray(111101) = 57: mimeArray(111110) = 43:
mimeArray(111111) = 47:
'determine input type
If srcIsFile Then
'File Manipulation
ff = FreeFile
Open strToEncode For Binary As #ff
ReDim charInput(FileLen(strToEncode) - 1)
Get #ff, , charInput
Close #ff
Else
'Store the string into ascii/byte array
charInput = StrConv(strToEncode, vbFromUnicode)
End If
'build array to help build main array
addArray(0) = 100000: addArray(1) = 10000: addArray(2) = 1000: addArray(3) = 100: addArray(4) = 10: addArray(5) = 1
addArray(6) = 100000: addArray(7) = 10000: addArray(8) = 1000: addArray(9) = 100: addArray(10) = 10: addArray(11) = 1
'build array to to hold the possibilities of each character
For b = 0 To 255
decVal = b
For c = 0 To 2
For d = 0 To 7
If (2 ^ (7 - d)) <= decVal Then
If c = 0 And d < 6 Then dblConvArray(b, 0, 0) = dblConvArray(b, 0, 0) + addArray(d)
If c = 0 And d > 5 Then dblConvArray(b, 0, 1) = dblConvArray(b, 0, 1) + addArray(d)
If c = 1 And d < 4 Then dblConvArray(b, 1, 1) = dblConvArray(b, 1, 1) + addArray(d + 2)
If c = 1 And d > 3 Then dblConvArray(b, 1, 2) = dblConvArray(b, 1, 2) + addArray(d + 2)
If c = 2 And d < 2 Then dblConvArray(b, 2, 2) = dblConvArray(b, 2, 2) + addArray(d + 4)
If c = 2 And d > 1 Then dblConvArray(b, 2, 3) = dblConvArray(b, 2, 3) + addArray(d + 4)
decVal = decVal - (2 ^ (7 - d))
End If
Next d
decVal = b
Next c
Next b
'destroy array
Erase addArray
'redim the output array to the calculated size
ReDim charOutput(((Int(UBound(charInput) / 3) * 4) + (UBound(charInput) Mod 3) + 1))
'now run a loop to check for line breaks and padding
If UBound(charOutput) + 1 > 72 Then
'resize the array to hold the total number of cr's and lf's
If ((UBound(charOutput) + 1) / 72) <> Int((UBound(charOutput) + 1) / 72) Then
ReDim Preserve charOutput((Int((UBound(charOutput) + 1) / 72) * 2) + UBound(charOutput))
Else
ReDim Preserve charOutput(((Int((UBound(charOutput) + 1) / 72) * 2) + UBound(charOutput)) - 2)
End If
're-pad the array
For z = 0 To padCount - 1
charOutput(UBound(charOutput) - z) = 61
Next z
Else
'the array isn't big enough to cr so just take care of padding
While (UBound(charOutput) + 1) / 4 <> Int((UBound(charOutput) + 1) / 4)
padCount = padCount + 1
ReDim Preserve charOutput(UBound(charOutput) + 1)
charOutput(UBound(charOutput)) = 61
Wend
End If
'clear y
y = 0
'start the main loop
For z = 0 To UBound(charInput) Step 3
'here we are actually inserting the cr and lf. We just accounted for them before. Padding was taken care of though
If z / 54 = Fix(z / 54) And z <> 0 Then
charOutput(y) = 13
charOutput(y + 1) = 10
y = y + 2
End If
'from here down...its greek...but basically we are adding the bits to get the 4 (6bit) chars from 3 (8bit) chars
charOutput(y) = mimeArray(dblConvArray(charInput(z), 0, 0))
If z + 1 > UBound(charInput) Then
charOutput(y + 1) = mimeArray(dblConvArray(charInput(z), 0, 1))
Exit For
End If
charOutput(y + 1) = mimeArray(dblConvArray(charInput(z), 0, 1) + dblConvArray(charInput(z + 1), 1, 1))
If z + 2 > UBound(charInput) Then
charOutput(y + 2) = mimeArray(dblConvArray(charInput(z + 1), 1, 2))
Exit For
End If
charOutput(y + 2) = mimeArray(dblConvArray(charInput(z + 1), 1, 2) + dblConvArray(charInput(z + 2), 2, 2))
charOutput(y + 3) = mimeArray(dblConvArray(charInput(z + 2), 2, 3))
y = y + 4
Next z
'destroy all arrays
Erase mimeArray
Erase dblConvArray
Erase charInput
'convert the byte array to string and return it
If writeFile Then
ff = FreeFile
If IsMissing(outFileName) Then
newFileName = strToEncode & ".encode"
Else
newFileName = outFileName
End If
Open newFileName For Output As #ff
Print #ff, "s"
Close #ff
Open newFileName For Binary As #ff
Put #ff, , charOutput
Close #ff
Else
'create a string for output
Encode = Left$(StrConv(charOutput, vbUnicode), UBound(charOutput) + 1)
End If
'destroy final array
Erase charOutput
End Function