Changeset 18342 for gsdl


Ignore:
Timestamp:
01/09/09 18:16:31 (12 years ago)
Author:
ak19
Message:

Added some base64 related methods (moved the call to MIME::base64 previously made in util.pm to here). The wrapper base64_encode method now helps to prevent re-encoding filenames using base64 several times over which was a little troubling before.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/unicode.pm

    r18338 r18342  
    3636use strict;
    3737use util;
     38use MIME::Base64; # for base64 encoding
    3839
    3940no strict 'refs';
     
    629630}
    630631
     632sub 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
     640sub 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.
     654sub 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 _
     662sub is_safe_filename_chars {
     663    my ($text) = @_;
     664    return ($text =~ m/^[A-Za-z0-9\+\/\=\-\_]+$/); #alphanumeric,[+/=-_]
     665}
    631666
    632667sub substr
Note: See TracChangeset for help on using the changeset viewer.