Last active
July 16, 2021 11:42
-
-
Save mwrouse/23d5fdf0512b6289e25df94f70ed4bd7 to your computer and use it in GitHub Desktop.
Inno Setup (Pascal Script) Base64 Decode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Left shift operator for Base64Decode (Inno Setup does not support << ) | |
function BitwiseLeftShift (input: LongInt; shiftFactor: Integer): LongInt; | |
var | |
exp: LongInt; // Total to multiply by | |
i: Integer; | |
//Result: LongInt; | |
begin | |
Result := 0; | |
exp := 1; | |
// Calculate 2^shiftFactor | |
for i := 1 to shiftFactor do | |
exp := exp * 2; | |
if exp = 0 then exp := 1; | |
Result := input * exp; | |
//BitwiseLeftShift := Result; | |
end; | |
// Right shift operator for Base64Decode (Inno Setup does not support >> ) | |
function BitwiseRightShift (input: LongInt; shiftFactor: Integer): LongInt; | |
var | |
exp: LongInt; // Total to multiply by | |
i: Integer; | |
//Result: LongInt; | |
begin | |
Result := 0; | |
exp := 1; | |
// Calculate 2^shiftFactor | |
for i := 1 to shiftFactor do | |
exp := exp * 2; | |
if exp = 0 then exp := 1; | |
Result := Trunc(input / exp); | |
//BitwiseRightShift := Result; | |
end; | |
function Base64Decode (input: String): String; | |
var | |
base64Alphabet: String; | |
c: String; | |
c2: String; | |
sum: LongInt; | |
i: Integer; | |
j: Integer; | |
bitModifier: Integer; | |
toProcess: String; // 4 characters to process | |
//Result: String; | |
begin | |
Result := ''; | |
base64Alphabet := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // Valid base64 characters | |
// Start decoding | |
toProcess := ''; | |
for i := 1 to (Length(input) + 1) do | |
begin | |
c := Copy(input, i, 1); | |
// Only process 4 characters at a time | |
if Length(toProcess) = 4 then | |
begin | |
sum := 0; | |
bitModifier := 18; | |
for j := 1 to Length(toProcess) do | |
begin | |
c2 := Copy(toProcess, j, 1); | |
sum := sum + BitwiseLeftShift((pos(c2, base64Alphabet) - 1), bitModifier); | |
bitModifier := bitModifier - 6; | |
end; | |
// Convert to characters and place into the result string | |
Insert(StringOfChar(Chr(BitwiseRightShift(sum, 16) and 255), 1), Result, Length(Result)+1); | |
Insert(StringOfChar(Chr(BitwiseRightShift(sum, 8) and 255), 1), Result, Length(Result)+1); | |
Insert(StringOfChar(Chr(sum and 255), 1), Result, Length(Result)+1); | |
toProcess := ''; // Reset the toProcess string | |
end; | |
// Add to string to be processed (replace padding with a 0 character) | |
if c <> '=' then | |
begin | |
if Pos(c, base64Alphabet) <> 0 then | |
begin | |
toProcess := toProcess + c; | |
end else if i <> (Length(input) + 1) then | |
begin | |
// Not base64 | |
Result := input; | |
break; | |
end; | |
end else | |
begin | |
toProcess := toProcess + 'A'; | |
end; | |
end; | |
//Base64Decode := Result; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment