weekly release 2.1.6+
[moodle.git] / lib / base32.php
blob831c71215fc3ec72633b5577794a7d8d86fdccb5
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | Base32 Library |
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 // +----------------------------------------------------------------------+
25 /**
26 * @package moodlecore
27 * @copyright (c) 2001 The PHP Group
28 * @license GNU GPL v2.0 http://www.php.net/license/2_02.txt
31 /**
32 * Base32 encode a binary string
34 * @param $inString Binary string to base32 encode
36 * @return $outString Base32 encoded $inString
38 * @access private
42 function base32_encode ($inString)
44 $outString = "";
45 $compBits = "";
46 $BASE32_TABLE = array(
47 '00000' => 0x61,
48 '00001' => 0x62,
49 '00010' => 0x63,
50 '00011' => 0x64,
51 '00100' => 0x65,
52 '00101' => 0x66,
53 '00110' => 0x67,
54 '00111' => 0x68,
55 '01000' => 0x69,
56 '01001' => 0x6a,
57 '01010' => 0x6b,
58 '01011' => 0x6c,
59 '01100' => 0x6d,
60 '01101' => 0x6e,
61 '01110' => 0x6f,
62 '01111' => 0x70,
63 '10000' => 0x71,
64 '10001' => 0x72,
65 '10010' => 0x73,
66 '10011' => 0x74,
67 '10100' => 0x75,
68 '10101' => 0x76,
69 '10110' => 0x77,
70 '10111' => 0x78,
71 '11000' => 0x79,
72 '11001' => 0x7a,
73 '11010' => 0x32,
74 '11011' => 0x33,
75 '11100' => 0x34,
76 '11101' => 0x35,
77 '11110' => 0x36,
78 '11111' => 0x37,
79 );
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]);
99 return $outString;
105 * Base32 decode to a binary string
107 * @param $inString String to base32 decode
109 * @return $outString Base32 decoded $inString
111 * @access private
115 function Base32_decode($inString) {
116 /* declaration */
117 $inputCheck = null;
118 $deCompBits = null;
120 $BASE32_TABLE = array(
121 0x61 => '00000',
122 0x62 => '00001',
123 0x63 => '00010',
124 0x64 => '00011',
125 0x65 => '00100',
126 0x66 => '00101',
127 0x67 => '00110',
128 0x68 => '00111',
129 0x69 => '01000',
130 0x6a => '01001',
131 0x6b => '01010',
132 0x6c => '01011',
133 0x6d => '01100',
134 0x6e => '01101',
135 0x6f => '01110',
136 0x70 => '01111',
137 0x71 => '10000',
138 0x72 => '10001',
139 0x73 => '10010',
140 0x74 => '10011',
141 0x75 => '10100',
142 0x76 => '10101',
143 0x77 => '10110',
144 0x78 => '10111',
145 0x79 => '11000',
146 0x7a => '11001',
147 0x32 => '11010',
148 0x33 => '11011',
149 0x34 => '11100',
150 0x35 => '11101',
151 0x36 => '11110',
152 0x37 => '11111',
155 /* Step 1 */
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);
159 return false;
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];
169 } else {
170 trigger_error('input to Base32Decode had a bad character: '.$inChar);
171 return false;
172 //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
173 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
177 /* Step 5 */
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');
182 return false;
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 */
188 $deArr = array();
189 for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++) {
190 $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
193 $outString = join('',$deArr);
195 return $outString;