Tech Tip: How to check whether a file is base64 encoded or not
PRODUCT: 4D | VERSION: 16/15/14/13/12/11 | PLATFORM: Mac & Win
Published On: October 19, 2016
There is a file without a file extension of ".BASE64" or ".B64" and the contents are suspected to be BASE64 (RFC2045 files) encoded. The utility method described in this Tech Tip and shown below will verify if the file contents are BASE64 encoded or not.
In BASE64 encoding, the character set is [A-Z, a-z, 0-9, + and /]. If the encoded length is not mod 4, the string is padded with '=' characters until it is mod 4.
The following regular expression pattern is used to check if a string is base64 encoded or not:...
^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$
Explained...
- ^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 4 character groups.
- ([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$ means the string ends in one of three forms: [A-Za-z0-9+/]{4} (four of the characters within the square brackets), [A-Za-z0-9+/]{3}= (three of the characters within the square brackets plus one equal sign) or [A-Za-z0-9+/]{2}== (two of the characters within the square brackets plus two equal signs).
// Name: STR_IsBASE64 // //method_parameters_declarations C_BOOLEAN($0;$Found_B) C_TEXT($Text_T;$1) //-------------------------------------------------------------------------------- //local_variable_declarations C_LONGINT($Ndx;$Params_L) C_TEXT($RegExPat_T) //====================== Initialize and Setup ================================ $Params_L:=Count parameters If ($Params_L>0) $Text_T:=$1 //======================== Method Actions ================================== $Ndx:=1 $RegExPat_T:="^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$" $Found_B:=Match regex($RegExPat_T;$Text_T;$Ndx) //======================== Clean up and Exit ================================= End if $0:=$Found_B |