As already mentioned here, there is no reliable check if a certain string is encoded by Base64 or not, so even if you consider the input as a valid Base64 string encoding, this does not mean that the string is actually encoded in this way, I am only posting another version of the verification function here which according to RFC 4648 confirms:
- if the input string is not empty and its length is a multiple of 4
- if the input line contains no more than two complementary characters and only at the end of the line
- if the input line contains only characters from the Base64 alphabet (see
Page 5, Table 1 )
function IsValidBase64EncodedString(const AValue: string): Boolean; const Base64Alphabet = ['A'..'Z', 'a'..'z', '0'..'9', '+', '/']; var I: Integer; ValLen: Integer; begin ValLen := Length(AValue); Result := (ValLen > 0) and (ValLen mod 4 = 0); if Result then begin while (AValue[ValLen] = '=') and (ValLen > Length(AValue) - 2) do Dec(ValLen); for I := ValLen downto 1 do if not (AValue[I] in Base64Alphabet) then begin Result := False; Break; end; end; end;
TLama source share