2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
36 require_once
'UtfNormalUtil.php';
43 # Load compatibility decompositions on demand if they are needed.
47 define(
'UNICODE_HANGUL_FIRST', 0xac00 );
48 define(
'UNICODE_HANGUL_LAST', 0xd7a3 );
50 define(
'UNICODE_HANGUL_LBASE', 0x1100 );
51 define(
'UNICODE_HANGUL_VBASE', 0x1161 );
52 define(
'UNICODE_HANGUL_TBASE', 0x11a7 );
54 define(
'UNICODE_HANGUL_LCOUNT', 19 );
55 define(
'UNICODE_HANGUL_VCOUNT', 21 );
56 define(
'UNICODE_HANGUL_TCOUNT', 28 );
63 define(
'UNICODE_SURROGATE_FIRST', 0xd800 );
64 define(
'UNICODE_SURROGATE_LAST', 0xdfff );
65 define(
'UNICODE_MAX', 0x10ffff );
66 define(
'UNICODE_REPLACEMENT', 0xfffd );
84 #define( 'UTF8_REPLACEMENT', '!' );
86 define(
'UTF8_OVERLONG_A',
"\xc1\xbf" );
87 define(
'UTF8_OVERLONG_B',
"\xe0\x9f\xbf" );
88 define(
'UTF8_OVERLONG_C',
"\xf0\x8f\xbf\xbf" );
90 # These two ranges are illegal
96 define(
'UTF8_HEAD',
false );
97 define(
'UTF8_TAIL',
true );
103 define(
'UNORM_NONE', 1 );
104 define(
'UNORM_NFD', 2 );
105 define(
'UNORM_NFKD', 3 );
106 define(
'UNORM_NFC', 4 );
108 define(
'UNORM_NFKC', 5 );
109 define(
'UNORM_FCD', 6 );
111 define(
'NORMALIZE_ICU', function_exists(
'utf8_normalize' ) );
130 # We exclude a few chars that ICU would not.
131 $string = preg_replace(
132 '/[\x00-\x08\x0b\x0c\x0e-\x1f]/',
138 # UnicodeString constructor fails if the string ends with a
139 # head byte. Add a junk char at the end, we'll strip it off.
140 return rtrim( utf8_normalize( $string .
"\x01",
UNORM_NFC ),
"\x01" );
142 # Side effect -- $string has had UTF-8 errors cleaned up.
159 return utf8_normalize( $string,
UNORM_NFC );
175 return utf8_normalize( $string,
UNORM_NFD );
176 elseif( preg_match(
'/[\x80-\xff]/', $string ) )
193 elseif( preg_match(
'/[\x80-\xff]/', $string ) )
210 elseif( preg_match(
'/[\x80-\xff]/', $string ) )
222 if( !isset( $utfCombiningClass ) ) {
223 require_once(
'UtfNormalData.inc' );
234 # ASCII is always valid NFC!
235 # If it's pure ASCII, let it through.
236 if( !preg_match(
'/[\x80-\xff]/', $string ) )
return true;
240 $len = strlen( $string );
241 for( $i = 0; $i < $len; $i++ ) {
246 } elseif( $n >= 0xf0 ) {
247 $c = substr( $string, $i, 4 );
249 } elseif( $n >= 0xe0 ) {
250 $c = substr( $string, $i, 3 );
252 } elseif( $n >= 0xc0 ) {
253 $c = substr( $string, $i, 2 );
256 if( isset( $utfCheckNFC[$c] ) ) {
257 # If it's NO or MAYBE, bail and do the slow check.
260 if( isset( $utfCombiningClass[$c] ) ) {
261 # Combining character? We might have to do sorting, at least.
274 # Screen out some characters that eg won't be allowed in XML
275 $string = preg_replace(
'/[\x00-\x08\x0b\x0c\x0e-\x1f]/',
UTF8_REPLACEMENT, $string );
277 # ASCII is always valid NFC!
278 # If we're only ever given plain ASCII, we can avoid the overhead
279 # of initializing the decomposition tables by skipping out early.
280 if( !preg_match(
'/[\x80-\xff]/', $string ) )
return true;
282 static $checkit = null, $tailBytes = null, $utfCheckOrCombining = null;
283 if( !isset( $checkit ) ) {
284 # Load/build some scary lookup tables...
288 $utfCheckOrCombining = array_merge( $utfCheckNFC, $utfCombiningClass );
290 # Head bytes for sequences which we should do further validity checks
291 $checkit = array_flip( array_map(
'chr',
292 array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
293 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
294 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff ) ) );
296 # Each UTF-8 head byte is followed by a certain
297 # number of tail bytes.
298 $tailBytes = array();
299 for( $n = 0; $n < 256; $n++ ) {
302 } elseif( $n < 0xe0 ) {
304 } elseif( $n < 0xf0 ) {
306 } elseif( $n < 0xf8 ) {
308 } elseif( $n < 0xfc ) {
310 } elseif( $n < 0xfe ) {
315 $tailBytes[chr($n)] = $remaining;
319 # Chop the text into pure-ASCII and non-ASCII areas;
320 # large ASCII parts can be handled much more quickly.
321 # Don't chop up Unicode areas for punctuation, though,
322 # that wastes energy.
324 '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
330 foreach( $matches[1] as $str ) {
331 $chunk = strlen( $str );
333 if( $str{0} <
"\x80" ) {
334 # ASCII chunk: guaranteed to be valid UTF-8
335 # and in normal form C, so skip over it.
340 # We'll have to examine the chunk byte by byte to ensure
341 # that it consists of valid UTF-8 sequences, and to see
342 # if any of them might not be normalized.
344 # Since PHP is not the fastest language on earth, some of
345 # this code is a little ugly with inner loop optimizations.
348 $len = $chunk + 1; # Counting down is faster. I
'm *so* sorry.
350 for( $i = -1; --$len; ) {
351 if( $remaining = $tailBytes[$c = $str{++$i}] ) {
353 $sequence = $head = $c;
355 # Look for the defined number of tail bytes...
356 if( --$len && ( $c = $str{++$i} ) >= "\x80" && $c < "\xc0" ) {
357 # Legal tail bytes are nice.
361 # Premature end of string!
362 # Drop a replacement character into output to
363 # represent the invalid UTF-8 sequence.
364 $replace[] = array( UTF8_REPLACEMENT,
365 $base + $i + 1 - strlen( $sequence ),
366 strlen( $sequence ) );
369 # Illegal tail byte; abandon the sequence.
370 $replace[] = array( UTF8_REPLACEMENT,
371 $base + $i - strlen( $sequence ),
372 strlen( $sequence ) );
373 # Back up and reprocess this byte; it may itself
374 # be a legal ASCII or UTF-8 sequence head.
380 } while( --$remaining );
382 if( isset( $checkit[$head] ) ) {
383 # Do some more detailed validity checks, for
384 # invalid characters and illegal sequences.
385 if( $head == "\xed" ) {
386 # 0xed is relatively frequent in Korean, which
387 # abuts the surrogate area, so we're doing
388 # this check separately to speed things up.
391 # Surrogates are legal only in UTF-16 code.
392 # They are totally forbidden here in UTF-8
395 $base + $i + 1 - strlen( $sequence ),
396 strlen( $sequence ) );
401 # Slower, but rarer checks...
404 #
"Overlong sequences" are those that are syntactically
405 # correct but use more UTF-8 bytes than are necessary to
406 # encode a character. Naïve
string comparisons can be
407 # tricked into failing to see a match
for an ASCII
408 # character,
for instance, which can be a security hole
409 #
if blacklist checks are being used.
414 # U+FFFE and U+FFFF are explicitly forbidden in Unicode.
419 # Unicode has been limited to 21 bits; longer
420 # sequences are not allowed.
421 || ($n >= 0xf0 && $sequence >
UTF8_MAX) ) {
424 $base + $i + 1 - strlen( $sequence ),
425 strlen( $sequence ) );
432 if( isset( $utfCheckOrCombining[$sequence] ) ) {
433 # If it's NO or MAYBE, we'll have to rip
434 # the string apart and put it back together.
435 # That's going to be mighty slow.
436 $looksNormal =
false;
439 # The sequence is legal!
441 } elseif( $c <
"\x80" ) {
444 } elseif( $c <
"\xc0" ) {
450 # Don't add if we're continuing a broken sequence;
451 # we already put a replacement character when we looked
452 # at the broken sequence.
453 $replace[] = array(
'', $base + $i, 1 );
456 # Miscellaneous freaks.
463 if( count( $replace ) ) {
464 # There were illegal UTF-8 sequences we need to fix up.
467 foreach( $replace as $rep ) {
468 list( $replacement, $start, $length ) = $rep;
469 if( $last < $start ) {
470 $out .= substr( $string, $last, $start - $last );
472 $out .= $replacement;
473 $last = $start + $length;
475 if( $last < strlen( $string ) ) {
476 $out .= substr( $string, $last );
483 # These take a string and run the normalization on them, without
484 # checking for validity or any optimization etc. Input must be
523 if( !isset( $utfCompatibilityDecomp ) ) {
524 require_once(
'UtfNormalDataK.inc' );
542 $len = strlen( $string );
544 for( $i = 0; $i < $len; $i++ ) {
548 # ASCII chars never decompose
552 } elseif( $n >= 0xf0 ) {
553 $c = substr( $string, $i, 4 );
555 } elseif( $n >= 0xe0 ) {
556 $c = substr( $string, $i, 3 );
558 } elseif( $n >= 0xc0 ) {
559 $c = substr( $string, $i, 2 );
562 if( isset( $map[$c] ) ) {
567 # Decompose a hangul syllable into jamo;
568 # hardcoded for three-byte UTF-8 sequence.
569 # A lookup table would be slightly faster,
570 # but adds a lot of memory & disk needs.
572 $index = ( (ord( $c{0} ) & 0x0f) << 12
573 | (ord( $c{1} ) & 0x3f) << 6
574 | (ord( $c{2} ) & 0x3f) )
579 $out .=
"\xe1\x84" . chr( 0x80 +
$l ) .
"\xe1\x85" . chr( 0xa1 + $v );
581 $out .=
"\xe1\x87" . chr( 0x80 +
$t - 25 );
583 $out .=
"\xe1\x86" . chr( 0xa7 +
$t );
603 $len = strlen( $string );
605 $combiners = array();
607 for( $i = 0; $i < $len; $i++ ) {
612 $c = substr( $string, $i, 4 );
614 } elseif( $n >= 0xe0 ) {
615 $c = substr( $string, $i, 3 );
617 } elseif( $n >= 0xc0 ) {
618 $c = substr( $string, $i, 2 );
621 if( isset( $utfCombiningClass[$c] ) ) {
622 $lastClass = $utfCombiningClass[$c];
623 @$combiners[$lastClass] .= $c;
629 $out .= implode(
'', $combiners );
630 $combiners = array();
637 $out .= implode(
'', $combiners );
652 $len = strlen( $string );
660 for( $i = 0; $i < $len; $i++ ) {
664 # No combining characters here...
671 } elseif( $n >= 0xf0 ) {
672 $c = substr( $string, $i, 4 );
674 } elseif( $n >= 0xe0 ) {
675 $c = substr( $string, $i, 3 );
677 } elseif( $n >= 0xc0 ) {
678 $c = substr( $string, $i, 2 );
681 $pair = $startChar . $c;
683 if( isset( $utfCombiningClass[$c] ) ) {
684 # A combining char; see what we can do with it
685 $class = $utfCombiningClass[$c];
686 if( !empty( $startChar ) &&
687 $lastClass < $class &&
689 isset( $utfCanonicalComp[$pair] ) ) {
690 $startChar = $utfCanonicalComp[$pair];
701 if( $lastClass == 0 ) {
702 if( isset( $utfCanonicalComp[$pair] ) ) {
703 $startChar = $utfCanonicalComp[$pair];
707 if( $n >= $x1 && $n <= $x2 ) {
708 # WARNING: Hangul code is painfully slow.
709 # I apologize for this ugly, ugly code; however
710 # performance is even more teh suck if we call
711 # out to nice clean functions. Lookup tables are
712 # marginally faster, but require a lot of space.
719 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
720 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
721 $lIndex = ord( $startChar{2} ) - 0x80;
722 $vIndex = ord( $c{2} ) - 0xa1;
728 # Hardcode the limited-range UTF-8 conversion:
729 $startChar = chr( $hangulPoint >> 12 & 0x0f | 0xe0 ) .
730 chr( $hangulPoint >> 6 & 0x3f | 0x80 ) .
731 chr( $hangulPoint & 0x3f | 0x80 );
739 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
740 $tIndex = ord( $c{2} ) - 0xa7;
741 if( $tIndex < 0 ) $tIndex = ord( $c{2} ) - 0x80 + (0x11c0 - 0x11a7);
743 # Increment the code point by $tIndex, without
744 # the function overhead of decoding and recoding UTF-8
746 $tail = ord( $startChar{2} ) + $tIndex;
749 $mid = ord( $startChar{1} ) + 1;
751 $startChar{0} = chr( ord( $startChar{0} ) + 1 );
754 $startChar{1} = chr( $mid );
756 $startChar{2} = chr( $tail );
758 # If there's another jamo char after this, *don't* try to merge it.
771 $out .= $startChar . $combining;
782 $len = strlen( $string );
784 for( $i = 0; $i < $len; $i++ ) {