3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 2001 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is dual-licensed. It is available under the terms |
9 // | of the GNU GPL v2.0 and under the terms of the PHP license version |
10 // | 2.02, available at through the world-wide-web at |
11 // | available at through the world-wide-web at |
12 // | http://www.php.net/license/2_02.txt. |
13 // +----------------------------------------------------------------------+
14 // | Minor fixes and additional functions by Allan Hansen. |
15 // | Moodle porting work by Martin Langhoff |
16 // +----------------------------------------------------------------------+
17 // | base32.php - based on race.php - RACE encode and decode strings. |
18 // +----------------------------------------------------------------------+
19 // | Authors: Allan Hansen <All@nHansen.dk> |
20 // | Arjan Wekking <a.wekking@synantics.nl> |
21 // | Martin Langhoff <martin@catalyst.net.nz> |
22 // +----------------------------------------------------------------------+
26 * Base32 encode a binary string
28 * @param $inString Binary string to base32 encode
30 * @return $outString Base32 encoded $inString
36 function base32_encode ($inString)
40 $BASE32_TABLE = array(
75 /* Turn the compressed string into a string that represents the bits as 0 and 1. */
76 for ($i = 0; $i < strlen($inString); $i++
) {
77 $compBits .= str_pad(decbin(ord(substr($inString,$i,1))), 8, '0', STR_PAD_LEFT
);
80 /* Pad the value with enough 0's to make it a multiple of 5 */
81 if((strlen($compBits) %
5) != 0) {
82 $compBits = str_pad($compBits, strlen($compBits)+
(5-(strlen($compBits)%5
)), '0', STR_PAD_RIGHT
);
85 /* Create an array by chunking it every 5 chars */
86 $fiveBitsArray = split("\n",rtrim(chunk_split($compBits, 5, "\n")));
88 /* Look-up each chunk and add it to $outstring */
89 foreach($fiveBitsArray as $fiveBitsString) {
90 $outString .= chr($BASE32_TABLE[$fiveBitsString]);
99 * Base32 decode to a binary string
101 * @param $inString String to base32 decode
103 * @return $outString Base32 decoded $inString
109 function Base32_decode($inString) {
114 $BASE32_TABLE = array(
150 $inputCheck = strlen($inString) %
8;
151 if(($inputCheck == 1)||
($inputCheck == 3)||
($inputCheck == 6)) {
152 trigger_error('input to Base32Decode was a bad mod length: '.$inputCheck);
154 //return $this->raiseError('input to Base32Decode was a bad mod length: '.$inputCheck, null,
155 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
158 /* $deCompBits is a string that represents the bits as 0 and 1.*/
159 for ($i = 0; $i < strlen($inString); $i++
) {
160 $inChar = ord(substr($inString,$i,1));
161 if(isset($BASE32_TABLE[$inChar])) {
162 $deCompBits .= $BASE32_TABLE[$inChar];
164 trigger_error('input to Base32Decode had a bad character: '.$inChar);
166 //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
167 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
172 $padding = strlen($deCompBits) %
8;
173 $paddingContent = substr($deCompBits, (strlen($deCompBits) - $padding));
174 if(substr_count($paddingContent, '1')>0) {
175 trigger_error('found non-zero padding in Base32Decode');
177 //return $this->raiseError('found non-zero padding in Base32Decode', null,
178 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
181 /* Break the decompressed string into octets for returning */
183 for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++
) {
184 $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
187 $outString = join('',$deArr);