Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / base32.php
blob7bb720ff646ea55938334de350d4754ac08f69bc
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 * Base32 encode a binary string
28 * @param $inString Binary string to base32 encode
30 * @return $outString Base32 encoded $inString
32 * @access private
36 function base32_encode ($inString)
38 $outString = "";
39 $compBits = "";
40 $BASE32_TABLE = array(
41 '00000' => 0x61,
42 '00001' => 0x62,
43 '00010' => 0x63,
44 '00011' => 0x64,
45 '00100' => 0x65,
46 '00101' => 0x66,
47 '00110' => 0x67,
48 '00111' => 0x68,
49 '01000' => 0x69,
50 '01001' => 0x6a,
51 '01010' => 0x6b,
52 '01011' => 0x6c,
53 '01100' => 0x6d,
54 '01101' => 0x6e,
55 '01110' => 0x6f,
56 '01111' => 0x70,
57 '10000' => 0x71,
58 '10001' => 0x72,
59 '10010' => 0x73,
60 '10011' => 0x74,
61 '10100' => 0x75,
62 '10101' => 0x76,
63 '10110' => 0x77,
64 '10111' => 0x78,
65 '11000' => 0x79,
66 '11001' => 0x7a,
67 '11010' => 0x32,
68 '11011' => 0x33,
69 '11100' => 0x34,
70 '11101' => 0x35,
71 '11110' => 0x36,
72 '11111' => 0x37,
73 );
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]);
93 return $outString;
98 /**
99 * Base32 decode to a binary string
101 * @param $inString String to base32 decode
103 * @return $outString Base32 decoded $inString
105 * @access private
109 function Base32_decode($inString) {
110 /* declaration */
111 $inputCheck = null;
112 $deCompBits = null;
114 $BASE32_TABLE = array(
115 0x61 => '00000',
116 0x62 => '00001',
117 0x63 => '00010',
118 0x64 => '00011',
119 0x65 => '00100',
120 0x66 => '00101',
121 0x67 => '00110',
122 0x68 => '00111',
123 0x69 => '01000',
124 0x6a => '01001',
125 0x6b => '01010',
126 0x6c => '01011',
127 0x6d => '01100',
128 0x6e => '01101',
129 0x6f => '01110',
130 0x70 => '01111',
131 0x71 => '10000',
132 0x72 => '10001',
133 0x73 => '10010',
134 0x74 => '10011',
135 0x75 => '10100',
136 0x76 => '10101',
137 0x77 => '10110',
138 0x78 => '10111',
139 0x79 => '11000',
140 0x7a => '11001',
141 0x32 => '11010',
142 0x33 => '11011',
143 0x34 => '11100',
144 0x35 => '11101',
145 0x36 => '11110',
146 0x37 => '11111',
149 /* Step 1 */
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);
153 return false;
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];
163 } else {
164 trigger_error('input to Base32Decode had a bad character: '.$inChar);
165 return false;
166 //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
167 // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
171 /* Step 5 */
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');
176 return false;
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 */
182 $deArr = array();
183 for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++) {
184 $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
187 $outString = join('',$deArr);
189 return $outString;