# http://www.nirvani.net/software/lib_b64 # # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License, Version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # DESCRIPTION: # # Do not get confused with base64, the popular # mime-encoding. lib_b64 has nothing to do # with the mime-encoding base64. However, it # is a base 64 encoding scheme. # # The idea behind it was to be able to visually represent # any number while taking up less visual space than the decimal, # hexidecimal or binary (any any base less than 64) while # also being able to have the resulting encoding # compatible with internet URLs. Specifically, # all characters in lib_b64 encoding do not require encoding # in URLs. The encoding includes "0-9", "a-z", "A-Z", "_" # and "." and nothing else. # # # PURPOSE: # # The purpose of this library is to reduce the # visible size of a number. # # Example 1: # # The binary string representaion of the integer 1,924,987,234,873 # (yes, that is nearly 2 trillion) is # '11100000000110010001011100010101000111001' # and can be reduced visually to the b64_charset() representation of # 'oazFru4' # # # Example 2: # # The hexidecimal representation of the md5() value of the string # 'hello world, I wish I could visit my favorite place for vacation', # which is a 128 bit number, is # '5eb63bbbe01eeed093cb22bb8f5acdc3' # and can be reduced visually to the b64_charset representation of # 'bg79AfkYWhLl1KBmk7Y.oe' # # # # PUBLIC INTERFACE: # # (string)b64_charset = b64_binto64( (string)binary_string ) # # (string)binary = b64_binfrom64( (string)b64_charset ) # # (string)binary = b64_hextobin (string)hexidecimal ) # # Define what the 64 characters in the # encoding character set will be. # I chose "0-9", "a-z", "A-Z", "." # and "_" because these are all characters # that do not require encoding for URLs. function b64_charset() { return array( chr(0x61), chr(0x41), chr(0x62), chr(0x42), chr(0x63), chr(0x43), chr(0x64), chr(0x44), chr(0x65), chr(0x45), chr(0x66), chr(0x46), chr(0x67), chr(0x47), chr(0x68), chr(0x48), chr(0x69), chr(0x49), chr(0x6A), chr(0x4A), chr(0x6B), chr(0x4B), chr(0x6C), chr(0x4C), chr(0x6D), chr(0x4D), chr(0x6E), chr(0x4E), chr(0x6F), chr(0x4F), chr(0x70), chr(0x50), chr(0x71), chr(0x51), chr(0x72), chr(0x52), chr(0x73), chr(0x53), chr(0x74), chr(0x54), chr(0x75), chr(0x55), chr(0x76), chr(0x56), chr(0x77), chr(0x57), chr(0x78), chr(0x58), chr(0x79), chr(0x59), chr(0x7A), chr(0x5A), chr(0x2E), chr(0x5F), chr(0x31), chr(0x32), chr(0x33), chr(0x34), chr(0x35), chr(0x36), chr(0x37), chr(0x38), chr(0x39), chr(0x30), ); } # This returns the reverse keys and values # of the b64_charset function. function b64_charset_rev() { $charset = b64_charset(); while(list($key, $val) = each($charset)) { $charset_rev[$val] = $key; } if (is_array($charset_rev) && count($charset_rev)) { return $charset_rev; } } # This function returns a string # of type b64_charset() and takes # a string consisting only of 1s and 0s. # On error, the function does not return. # Zeros (not literally '0', but the # defined zero value from b64_charset(), # get rounded off. function b64_binto64($binstr) { $charset = b64_charset(); if (!ereg('[^0^1]+',$binstr)) { $binstr = ereg_replace('^0+','0',$binstr); $size = strlen($binstr); $count = 0; $shortbin = ''; for ($i=$size-1; $i>=0; $i--) { $count++; $shortbin = $binstr{$i} . $shortbin; if ($count % 6 == 0 || $i == 0) { $long64 = $charset[bindec($shortbin)] . $long64; $shortbin = ''; } } if (strlen($long64)) { $ret = TRUE; } $long64 = ereg_replace('^'.$charset[0].'+','',$long64); if (strlen($long64)) { return $long64; } else if ($ret) { return $charset[0]; } } } # This function returns a string # consisting of 1s and 0s (a binary string) # iff the input is a string consisting # of b64_charset(). On error # this function does not return. # Zeros get rounded off. function b64_binfrom64($str64) { $charset_rev = b64_charset_rev(); while(list($key, $val) = each($charset_rev)) { $checkstr .= '^'.$key; } if (!ereg('['.$checkstr.']+', $str64)) { $str64 = ereg_replace('^'.$charset_rev[0].'+','',$str64); $size = strlen($str64); $strbin = ''; for ($i=$size-1; $i>=0; $i--) { $strbin = sprintf('%06d',decbin($charset_rev[$str64{$i}])) . $strbin; } if (strlen($strbin)) { $ret = TRUE; } $strbin = ereg_replace('^0+','',$strbin); if (strlen($strbin)) { return $strbin; } else if ($ret) { return '0'; } } } # This function returns a string # of 1s and 0s (a binary string) # if the input is of type Hexidecimal, # that is "0-9" and "a-f". Capitalization # of the hexidecimal does not matter. # on error the function does not return. # Zeros are rounded off. function b64_hextobin($hex) { $hex = strtolower($hex); if (!ereg('[^0^1^2^3^4^5^6^7^8^9^a^b^c^d^e^f]+', $hex)) { $hex = ereg_replace('^0+','0',$hex); $size = strlen($hex); $strbin = ''; for ($i=$size-1; $i>=0; $i--) { $strbin = sprintf('%04d',decbin(hexdec($hex{$i}))) . $strbin; } if (strlen($strbin)) { $ret = TRUE; } $strbin = ereg_replace('^0+','',$strbin); if (strlen($strbin)) { return $strbin; } else if ($ret) { return '0'; } } } ?>