fix image text
[pear] / Base32.php
1 <?php
2
3 /**
4  * NTICompass' crappy base32 library for PHP
5  * 
6  * http://labs.nticompassinc.com
7  */
8 class Base32 {
9
10     var $encode, $decode, $type;
11
12     // Supports RFC 4648 (default) or Crockford (http://www.crockford.com/wrmg/base32.html)
13     function __construct($alphabet = 'rfc4648') 
14     {
15         $alphabet = strtolower($alphabet);
16         $this->type = $alphabet;
17         // Crockford's alphabet removes I,L,O, and U
18         $crockfordABC = range('A', 'Z');
19         unset($crockfordABC[8], $crockfordABC[11], $crockfordABC[14], $crockfordABC[20]);
20         $crockfordABC = array_values($crockfordABC);
21
22         $alphabets = array(
23             'rfc4648' => array_merge(range('A', 'Z'), range(2, 7), array('=')),
24             'crockford' => array_merge(range(0, 9), $crockfordABC, array('='))
25         );
26         $this->encode = $alphabets[$alphabet];
27         $this->decode = array_flip($this->encode);
28         // Add extra letters for Crockford's alphabet
29         if ($alphabet === 'crockford') {
30             $this->decode['O'] = 0;
31             $this->decode['I'] = 1;
32             $this->decode['L'] = 1;
33         }
34     }
35
36     private function bin_chunk($binaryString, $bits) 
37     {
38         $binaryString = chunk_split($binaryString, $bits, ' ');
39         if ($this->endsWith($binaryString, ' ')) {
40             $binaryString = substr($binaryString, 0, strlen($binaryString) - 1);
41         }
42         return explode(' ', $binaryString);
43     }
44
45     // String <-> Binary conversion
46     // Based off: http://psoug.org/snippet/PHP-Binary-to-Text-Text-to-Binary_380.htm
47
48     private function bin2str($binaryString) 
49     {
50         // Make sure binary string is in 8-bit chunks
51         $binaryArray = $this->bin_chunk($binaryString, 8);
52         $string = '';
53         foreach ($binaryArray as $bin) {
54             // Pad each value to 8 bits
55             $bin = str_pad($bin, 8, 0, STR_PAD_RIGHT);
56             // Convert binary strings to ascii
57             $string .= chr(bindec($bin));
58         }
59         return $string;
60     }
61
62     private function str2bin($input) 
63     {
64         $bin = '';
65         foreach (str_split($input) as $s) {
66             // Return each character as an 8-bit binary string
67             $s = decbin(ord($s));
68             $bin .= str_pad($s, 8, 0, STR_PAD_LEFT);
69         }
70         return $bin;
71     }
72
73     // starts/endsWith from:
74     // http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions/834355#834355
75
76     private function startsWith($haystack, $needle) 
77     {
78         $length = strlen($needle);
79         return substr($haystack, 0, $length) === $needle;
80     }
81
82     private function endsWith($haystack, $needle) 
83     {
84         $length = strlen($needle);
85         return substr($haystack, -$length) === $needle;
86     }
87
88     // base32 info from: http://www.garykessler.net/library/base64.html
89     // base32_encode
90     public function base32_encode($string) 
91     {
92         // Convert string to binary
93         $binaryString = $this->str2bin($string);
94
95         // Break into 5-bit chunks, then break that into an array
96         $binaryArray = $this->bin_chunk($binaryString, 5);
97
98         // Pad array to be divisible by 8
99         while (count($binaryArray) % 8 !== 0) {
100             $binaryArray[] = null;
101         }
102
103         $base32String = '';
104
105         // Encode in base32
106         foreach ($binaryArray as $bin) {
107             $char = 32;
108             if (!is_null($bin)) {
109                 // Pad the binary strings
110                 $bin = str_pad($bin, 5, 0, STR_PAD_RIGHT);
111                 $char = bindec($bin);
112             }
113             // Base32 character
114             $base32String .= $this->encode[$char];
115         }
116
117         return $base32String;
118     }
119
120     // base32_decode
121     public function base32_decode($base32String) 
122     {
123         $base32Array = str_split(str_replace('-', '', strtoupper($base32String)));
124         $binaryArray = array();
125         $string = '';
126         foreach ($base32Array as $str) {
127             $char = $this->decode[$str];
128             if ($char !== 32) {
129                 $char = decbin($char);
130                 $string .= str_pad($char, 5, 0, STR_PAD_LEFT);
131             }
132         }
133         while (strlen($string) % 8 !== 0) {
134             $string = substr($string, 0, strlen($string) - 1);
135         }
136         return $this->bin2str($string);
137     }
138
139 }
140
141 ?>