| 632 | sub base64_encode { |
| 633 | my ($text) = @_; |
| 634 | if(!is_safe_filename_chars($text)) { #if(!&is_base64_or_alphanumeric($text)) { # a subset |
| 635 | $text = &MIME::Base64::encode_base64($text); |
| 636 | } |
| 637 | return $text; |
| 638 | } |
| 639 | |
| 640 | sub base64_decode { |
| 641 | my ($text) = @_; |
| 642 | # If the input fits the base64 pattern, this will try decoding it. |
| 643 | # Still, this does not guarantee the return value is the 'original' |
| 644 | if(&is_base64_or_alphanumeric($text)) { |
| 645 | $text = &MIME::Base64::decode_base64($text); |
| 646 | } |
| 647 | return $text; |
| 648 | } |
| 649 | |
| 650 | # Returns through if the given string is compatible with base64 (which |
| 651 | # includes regular ASCII alphanumeric values). This method does not |
| 652 | # guarantee that base64_decoding will return anything meanigful, since |
| 653 | # this will return true for any simple alphanumeric ASCII string as well. |
| 654 | sub is_base64_or_alphanumeric { |
| 655 | my ($text) = @_; |
| 656 | # base 64 takes alphanumeric and [+/=] |
| 657 | # (note that in some cases - and _ replace + and /) |
| 658 | return ($text =~ m/^[A-Za-z0-9\+\/\=]+$/); |
| 659 | } |
| 660 | |
| 661 | # Returns true if the text is base64 chars or additionally contains - and/or _ |
| 662 | sub is_safe_filename_chars { |
| 663 | my ($text) = @_; |
| 664 | return ($text =~ m/^[A-Za-z0-9\+\/\=\-\_]+$/); #alphanumeric,[+/=-_] |
| 665 | } |