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 // +----------------------------------------------------------------------+
27 * @copyright (c) 2001 The PHP Group
28 * @license GNU GPL v2.0 http://www.php.net/license/2_02.txt
32 * Base32 encode a binary string
34 * @param $inString Binary string to base32 encode
36 * @return $outString Base32 encoded $inString
42 function base32_encode ($inString)
46 $BASE32_TABLE = array(
81 /* Turn the compressed string into a string that represents the bits as 0 and 1. */
82 for ($i = 0; $i < strlen($inString); $i++
) {
83 $compBits .= str_pad(decbin(ord(substr($inString,$i,1))), 8, '0', STR_PAD_LEFT
);
86 /* Pad the value with enough 0's to make it a multiple of 5 */
87 if((strlen($compBits) %
5) != 0) {
88 $compBits = str_pad($compBits, strlen($compBits)+
(5-(strlen($compBits)%5
)), '0', STR_PAD_RIGHT
);
91 /* Create an array by chunking it every 5 chars */
92 $fiveBitsArray = explode("\n",rtrim(chunk_split($compBits, 5, "\n")));
94 /* Look-up each chunk and add it to $outstring */
95 foreach($fiveBitsArray as $fiveBitsString) {
96 $outString .= chr($BASE32_TABLE[$fiveBitsString]);
105 * Base32 decode to a binary string
107 * @param $inString String to base32 decode
109 * @return $outString Base32 decoded $inString
115 function Base32_decode($inString) {
120 $BASE32_TABLE = array(
156 $inputCheck = strlen($inString) %
8;
157 if(($inputCheck == 1)||
($inputCheck == 3)||
($inputCheck == 6)) {
158 trigger_error('input to Base32Decode was a bad mod length: '.$inputCheck);
160 //return $this->raiseError('input to Base32Decode was a bad mod length: '.$inputCheck, null,
161 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
164 /* $deCompBits is a string that represents the bits as 0 and 1.*/
165 for ($i = 0; $i < strlen($inString); $i++
) {
166 $inChar = ord(substr($inString,$i,1));
167 if(isset($BASE32_TABLE[$inChar])) {
168 $deCompBits .= $BASE32_TABLE[$inChar];
170 trigger_error('input to Base32Decode had a bad character: '.$inChar);
172 //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
173 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
178 $padding = strlen($deCompBits) %
8;
179 $paddingContent = substr($deCompBits, (strlen($deCompBits) - $padding));
180 if(substr_count($paddingContent, '1')>0) {
181 trigger_error('found non-zero padding in Base32Decode');
183 //return $this->raiseError('found non-zero padding in Base32Decode', null,
184 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
187 /* Break the decompressed string into octets for returning */
189 for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++
) {
190 $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
193 $outString = join('',$deArr);