view e2gallerypro/e2upload/Backend/Assets/getid3/module.lib.iconv_replacement.php @ 28:118cb614cf18 judyates tip

judy bio.
author Robert McIntyre <rlm@mit.edu>
date Tue, 29 Dec 2015 16:26:18 -0800
parents 3f6b44aa6b35
children
line wrap: on
line source
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP version 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2 of the GPL license, |
8 // | that is bundled with this package in the file license.txt and is |
9 // | available through the world-wide-web at the following url: |
10 // | http://www.gnu.org/copyleft/gpl.html |
11 // +----------------------------------------------------------------------+
12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
13 // +----------------------------------------------------------------------+
14 // | Authors: James Heinrich <infoØgetid3*org> |
15 // | Allan Hansen <ahØartemis*dk> |
16 // +----------------------------------------------------------------------+
17 // | module.lib.iconv_replacement.php |
18 // | getID3() library file. |
19 // | dependencies: NONE, required by getid3.php if no iconv() present. |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: module.lib.iconv_replacement.php,v 1.4 2006/11/02 10:48:02 ah Exp $
25 class getid3_iconv_replacement
26 {
28 public static function iconv($in_charset, $out_charset, $string) {
30 if ($in_charset == $out_charset) {
31 return $string;
32 }
34 static $supported_charsets = array (
35 'ISO-8859-1' => 'iso88591',
36 'UTF-8' => 'utf8',
37 'UTF-16BE' => 'utf16be',
38 'UTF-16LE' => 'utf16le',
39 'UTF-16' => 'utf16'
40 );
42 // Convert
43 $function_name = 'iconv_' . @$supported_charsets[$in_charset] . '_' . @$supported_charsets[$out_charset];
45 if (is_callable(array('getid3_iconv_replacement', $function_name))) {
46 return getid3_iconv_replacement::$function_name($string);
47 }
49 // Invalid charset used
50 if (!@$supported_charsets[$in_charset]) {
51 throw new getid3_exception('PHP does not have iconv() support - cannot use ' . $in_charset . ' charset.');
52 }
54 if (!@$supported_charsets[$out_charset]) {
55 throw new getid3_exception('PHP does not have iconv() support - cannot use ' . $out_charset . ' charset.');
56 }
57 }
61 public static function iconv_int_utf8($charval) {
62 if ($charval < 128) {
63 // 0bbbbbbb
64 $newcharstring = chr($charval);
65 } elseif ($charval < 2048) {
66 // 110bbbbb 10bbbbbb
67 $newcharstring = chr(($charval >> 6) | 0xC0);
68 $newcharstring .= chr(($charval & 0x3F) | 0x80);
69 } elseif ($charval < 65536) {
70 // 1110bbbb 10bbbbbb 10bbbbbb
71 $newcharstring = chr(($charval >> 12) | 0xE0);
72 $newcharstring .= chr(($charval >> 6) | 0xC0);
73 $newcharstring .= chr(($charval & 0x3F) | 0x80);
74 } else {
75 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
76 $newcharstring = chr(($charval >> 18) | 0xF0);
77 $newcharstring .= chr(($charval >> 12) | 0xC0);
78 $newcharstring .= chr(($charval >> 6) | 0xC0);
79 $newcharstring .= chr(($charval & 0x3F) | 0x80);
80 }
81 return $newcharstring;
82 }
86 // ISO-8859-1 => UTF-8
87 public static function iconv_iso88591_utf8($string, $bom=false) {
88 if (function_exists('utf8_encode')) {
89 return utf8_encode($string);
90 }
91 // utf8_encode() unavailable, use getID3()'s iconv() conversions (possibly PHP is compiled without XML support)
92 $newcharstring = '';
93 if ($bom) {
94 $newcharstring .= "\xEF\xBB\xBF";
95 }
96 for ($i = 0; $i < strlen($string); $i++) {
97 $charval = ord($string{$i});
98 $newcharstring .= getid3_iconv_replacement::iconv_int_utf8($charval);
99 }
100 return $newcharstring;
101 }
105 // ISO-8859-1 => UTF-16BE
106 public static function iconv_iso88591_utf16be($string, $bom=false) {
107 $newcharstring = '';
108 if ($bom) {
109 $newcharstring .= "\xFE\xFF";
110 }
111 for ($i = 0; $i < strlen($string); $i++) {
112 $newcharstring .= "\x00".$string{$i};
113 }
114 return $newcharstring;
115 }
119 // ISO-8859-1 => UTF-16LE
120 public static function iconv_iso88591_utf16le($string, $bom=false) {
121 $newcharstring = '';
122 if ($bom) {
123 $newcharstring .= "\xFF\xFE";
124 }
125 for ($i = 0; $i < strlen($string); $i++) {
126 $newcharstring .= $string{$i}."\x00";
127 }
128 return $newcharstring;
129 }
133 // ISO-8859-1 => UTF-16
134 public static function iconv_iso88591_utf16($string) {
135 return getid3_lib::iconv_iso88591_utf16le($string, true);
136 }
140 // UTF-8 => ISO-8859-1
141 public static function iconv_utf8_iso88591($string) {
142 if (function_exists('utf8_decode')) {
143 return utf8_decode($string);
144 }
145 // utf8_decode() unavailable, use getID3()'s iconv() conversions (possibly PHP is compiled without XML support)
146 $newcharstring = '';
147 $offset = 0;
148 $stringlength = strlen($string);
149 while ($offset < $stringlength) {
150 if ((ord($string{$offset}) | 0x07) == 0xF7) {
151 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
152 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
153 ((ord($string{($offset + 1)}) & 0x3F) << 12) &
154 ((ord($string{($offset + 2)}) & 0x3F) << 6) &
155 (ord($string{($offset + 3)}) & 0x3F);
156 $offset += 4;
157 } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
158 // 1110bbbb 10bbbbbb 10bbbbbb
159 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
160 ((ord($string{($offset + 1)}) & 0x3F) << 6) &
161 (ord($string{($offset + 2)}) & 0x3F);
162 $offset += 3;
163 } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
164 // 110bbbbb 10bbbbbb
165 $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
166 (ord($string{($offset + 1)}) & 0x3F);
167 $offset += 2;
168 } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
169 // 0bbbbbbb
170 $charval = ord($string{$offset});
171 $offset += 1;
172 } else {
173 // error? throw some kind of warning here?
174 $charval = false;
175 $offset += 1;
176 }
177 if ($charval !== false) {
178 $newcharstring .= (($charval < 256) ? chr($charval) : '?');
179 }
180 }
181 return $newcharstring;
182 }
186 // UTF-8 => UTF-16BE
187 public static function iconv_utf8_utf16be($string, $bom=false) {
188 $newcharstring = '';
189 if ($bom) {
190 $newcharstring .= "\xFE\xFF";
191 }
192 $offset = 0;
193 $stringlength = strlen($string);
194 while ($offset < $stringlength) {
195 if ((ord($string{$offset}) | 0x07) == 0xF7) {
196 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
197 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
198 ((ord($string{($offset + 1)}) & 0x3F) << 12) &
199 ((ord($string{($offset + 2)}) & 0x3F) << 6) &
200 (ord($string{($offset + 3)}) & 0x3F);
201 $offset += 4;
202 } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
203 // 1110bbbb 10bbbbbb 10bbbbbb
204 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
205 ((ord($string{($offset + 1)}) & 0x3F) << 6) &
206 (ord($string{($offset + 2)}) & 0x3F);
207 $offset += 3;
208 } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
209 // 110bbbbb 10bbbbbb
210 $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
211 (ord($string{($offset + 1)}) & 0x3F);
212 $offset += 2;
213 } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
214 // 0bbbbbbb
215 $charval = ord($string{$offset});
216 $offset += 1;
217 } else {
218 // error? throw some kind of warning here?
219 $charval = false;
220 $offset += 1;
221 }
222 if ($charval !== false) {
223 $newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
224 }
225 }
226 return $newcharstring;
227 }
231 // UTF-8 => UTF-16LE
232 public static function iconv_utf8_utf16le($string, $bom=false) {
233 $newcharstring = '';
234 if ($bom) {
235 $newcharstring .= "\xFF\xFE";
236 }
237 $offset = 0;
238 $stringlength = strlen($string);
239 while ($offset < $stringlength) {
240 if ((ord($string{$offset}) | 0x07) == 0xF7) {
241 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
242 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
243 ((ord($string{($offset + 1)}) & 0x3F) << 12) &
244 ((ord($string{($offset + 2)}) & 0x3F) << 6) &
245 (ord($string{($offset + 3)}) & 0x3F);
246 $offset += 4;
247 } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
248 // 1110bbbb 10bbbbbb 10bbbbbb
249 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
250 ((ord($string{($offset + 1)}) & 0x3F) << 6) &
251 (ord($string{($offset + 2)}) & 0x3F);
252 $offset += 3;
253 } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
254 // 110bbbbb 10bbbbbb
255 $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
256 (ord($string{($offset + 1)}) & 0x3F);
257 $offset += 2;
258 } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
259 // 0bbbbbbb
260 $charval = ord($string{$offset});
261 $offset += 1;
262 } else {
263 // error? maybe throw some warning here?
264 $charval = false;
265 $offset += 1;
266 }
267 if ($charval !== false) {
268 $newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
269 }
270 }
271 return $newcharstring;
272 }
276 // UTF-8 => UTF-16
277 public static function iconv_utf8_utf16($string) {
278 return getid3_lib::iconv_utf8_utf16le($string, true);
279 }
283 // UTF-16BE => ISO-8859-1
284 public static function iconv_utf16be_iso88591($string) {
285 if (substr($string, 0, 2) == "\xFE\xFF") {
286 // strip BOM
287 $string = substr($string, 2);
288 }
289 $newcharstring = '';
290 for ($i = 0; $i < strlen($string); $i += 2) {
291 $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
292 $newcharstring .= (($charval < 256) ? chr($charval) : '?');
293 }
294 return $newcharstring;
295 }
299 // UTF-16BE => UTF-8
300 public static function iconv_utf16be_utf8($string) {
301 if (substr($string, 0, 2) == "\xFE\xFF") {
302 // strip BOM
303 $string = substr($string, 2);
304 }
305 $newcharstring = '';
306 for ($i = 0; $i < strlen($string); $i += 2) {
307 $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
308 $newcharstring .= getid3_iconv_replacement::iconv_int_utf8($charval);
309 }
310 return $newcharstring;
311 }
315 // UTF-16BE => UTF-16LE
316 public static function iconv_utf16be_utf16le($string) {
317 return getid3_iconv_replacement::iconv_utf8_utf16le(getid3_iconv_replacement::iconv_utf16be_utf8($string));
318 }
322 // UTF-16BE => UTF-16
323 public static function iconv_utf16be_utf16($string) {
324 return getid3_iconv_replacement::iconv_utf8_utf16(getid3_iconv_replacement::iconv_utf16be_utf8($string));
325 }
329 // UTF-16LE => ISO-8859-1
330 public static function iconv_utf16le_iso88591($string) {
331 if (substr($string, 0, 2) == "\xFF\xFE") {
332 // strip BOM
333 $string = substr($string, 2);
334 }
335 $newcharstring = '';
336 for ($i = 0; $i < strlen($string); $i += 2) {
337 $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
338 $newcharstring .= (($charval < 256) ? chr($charval) : '?');
339 }
340 return $newcharstring;
341 }
345 // UTF-16LE => UTF-8
346 public static function iconv_utf16le_utf8($string) {
347 if (substr($string, 0, 2) == "\xFF\xFE") {
348 // strip BOM
349 $string = substr($string, 2);
350 }
351 $newcharstring = '';
352 for ($i = 0; $i < strlen($string); $i += 2) {
353 $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
354 $newcharstring .= getid3_iconv_replacement::iconv_int_utf8($charval);
355 }
356 return $newcharstring;
357 }
361 // UTF-16LE => UTF-16BE
362 public static function iconv_utf16le_utf16be($string) {
363 return getid3_iconv_replacement::iconv_utf8_utf16be(getid3_iconv_replacement::iconv_utf16le_utf8($string));
364 }
368 // UTF-16LE => UTF-16
369 public static function iconv_utf16le_utf16($string) {
370 return getid3_iconv_replacement::iconv_utf8_utf16(getid3_iconv_replacement::iconv_utf16le_utf8($string));
371 }
375 // UTF-16 => ISO-8859-1
376 public static function iconv_utf16_iso88591($string) {
377 $bom = substr($string, 0, 2);
378 if ($bom == "\xFE\xFF") {
379 return getid3_lib::iconv_utf16be_iso88591(substr($string, 2));
380 } elseif ($bom == "\xFF\xFE") {
381 return getid3_lib::iconv_utf16le_iso88591(substr($string, 2));
382 }
383 return $string;
384 }
388 // UTF-16 => UTF-8
389 public static function iconv_utf16_utf8($string) {
390 $bom = substr($string, 0, 2);
391 if ($bom == "\xFE\xFF") {
392 return getid3_iconv_replacement::iconv_utf16be_utf8(substr($string, 2));
393 } elseif ($bom == "\xFF\xFE") {
394 return getid3_iconv_replacement::iconv_utf16le_utf8(substr($string, 2));
395 }
396 return $string;
397 }
401 // UTF-16 => UTF-16BE
402 public static function iconv_utf16_utf16be($string) {
403 return getid3_iconv_replacement::iconv_utf8_utf16be(getid3_iconv_replacement::iconv_utf16_utf8($string));
404 }
408 // UTF-16 => UTF-16LE
409 public static function iconv_utf16_utf16le($string) {
410 return getid3_iconv_replacement::iconv_utf8_utf16le(getid3_iconv_replacement::iconv_utf16_utf8($string));
411 }
413 }
415 ?>