File indexing completed on 2024-05-05 06:02:16

0001 <?php
0002 
0003 /**
0004  * A UTF-8 specific character encoder that handles cleaning and transforming.
0005  * @note All functions in this class should be static.
0006  */
0007 class HTMLPurifier_Encoder
0008 {
0009 
0010     /**
0011      * Constructor throws fatal error if you attempt to instantiate class
0012      */
0013     private function __construct()
0014     {
0015         trigger_error('Cannot instantiate encoder, call methods statically', E_USER_ERROR);
0016     }
0017 
0018     /**
0019      * Error-handler that mutes errors, alternative to shut-up operator.
0020      */
0021     public static function muteErrorHandler()
0022     {
0023     }
0024 
0025     /**
0026      * iconv wrapper which mutes errors, but doesn't work around bugs.
0027      * @param string $in Input encoding
0028      * @param string $out Output encoding
0029      * @param string $text The text to convert
0030      * @return string
0031      */
0032     public static function unsafeIconv($in, $out, $text)
0033     {
0034         set_error_handler(array('HTMLPurifier_Encoder', 'muteErrorHandler'));
0035         $r = iconv($in, $out, $text);
0036         restore_error_handler();
0037         return $r;
0038     }
0039 
0040     /**
0041      * iconv wrapper which mutes errors and works around bugs.
0042      * @param string $in Input encoding
0043      * @param string $out Output encoding
0044      * @param string $text The text to convert
0045      * @param int $max_chunk_size
0046      * @return string
0047      */
0048     public static function iconv($in, $out, $text, $max_chunk_size = 8000)
0049     {
0050         $code = self::testIconvTruncateBug();
0051         if ($code == self::ICONV_OK) {
0052             return self::unsafeIconv($in, $out, $text);
0053         } elseif ($code == self::ICONV_TRUNCATES) {
0054             // we can only work around this if the input character set
0055             // is utf-8
0056             if ($in == 'utf-8') {
0057                 if ($max_chunk_size < 4) {
0058                     trigger_error('max_chunk_size is too small', E_USER_WARNING);
0059                     return false;
0060                 }
0061                 // split into 8000 byte chunks, but be careful to handle
0062                 // multibyte boundaries properly
0063                 if (($c = strlen($text)) <= $max_chunk_size) {
0064                     return self::unsafeIconv($in, $out, $text);
0065                 }
0066                 $r = '';
0067                 $i = 0;
0068                 while (true) {
0069                     if ($i + $max_chunk_size >= $c) {
0070                         $r .= self::unsafeIconv($in, $out, substr($text, $i));
0071                         break;
0072                     }
0073                     // wibble the boundary
0074                     if (0x80 != (0xC0 & ord($text[$i + $max_chunk_size]))) {
0075                         $chunk_size = $max_chunk_size;
0076                     } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 1]))) {
0077                         $chunk_size = $max_chunk_size - 1;
0078                     } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 2]))) {
0079                         $chunk_size = $max_chunk_size - 2;
0080                     } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 3]))) {
0081                         $chunk_size = $max_chunk_size - 3;
0082                     } else {
0083                         return false; // rather confusing UTF-8...
0084                     }
0085                     $chunk = substr($text, $i, $chunk_size); // substr doesn't mind overlong lengths
0086                     $r .= self::unsafeIconv($in, $out, $chunk);
0087                     $i += $chunk_size;
0088                 }
0089                 return $r;
0090             } else {
0091                 return false;
0092             }
0093         } else {
0094             return false;
0095         }
0096     }
0097 
0098     /**
0099      * Cleans a UTF-8 string for well-formedness and SGML validity
0100      *
0101      * It will parse according to UTF-8 and return a valid UTF8 string, with
0102      * non-SGML codepoints excluded.
0103      *
0104      * Specifically, it will permit:
0105      * \x{9}\x{A}\x{D}\x{20}-\x{7E}\x{A0}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}
0106      * Source: https://www.w3.org/TR/REC-xml/#NT-Char
0107      * Arguably this function should be modernized to the HTML5 set
0108      * of allowed characters:
0109      * https://www.w3.org/TR/html5/syntax.html#preprocessing-the-input-stream
0110      * which simultaneously expand and restrict the set of allowed characters.
0111      *
0112      * @param string $str The string to clean
0113      * @param bool $force_php
0114      * @return string
0115      *
0116      * @note Just for reference, the non-SGML code points are 0 to 31 and
0117      *       127 to 159, inclusive.  However, we allow code points 9, 10
0118      *       and 13, which are the tab, line feed and carriage return
0119      *       respectively. 128 and above the code points map to multibyte
0120      *       UTF-8 representations.
0121      *
0122      * @note Fallback code adapted from utf8ToUnicode by Henri Sivonen and
0123      *       hsivonen@iki.fi at <http://iki.fi/hsivonen/php-utf8/> under the
0124      *       LGPL license.  Notes on what changed are inside, but in general,
0125      *       the original code transformed UTF-8 text into an array of integer
0126      *       Unicode codepoints. Understandably, transforming that back to
0127      *       a string would be somewhat expensive, so the function was modded to
0128      *       directly operate on the string.  However, this discourages code
0129      *       reuse, and the logic enumerated here would be useful for any
0130      *       function that needs to be able to understand UTF-8 characters.
0131      *       As of right now, only smart lossless character encoding converters
0132      *       would need that, and I'm probably not going to implement them.
0133      */
0134     public static function cleanUTF8($str, $force_php = false)
0135     {
0136         // UTF-8 validity is checked since PHP 4.3.5
0137         // This is an optimization: if the string is already valid UTF-8, no
0138         // need to do PHP stuff. 99% of the time, this will be the case.
0139         if (preg_match(
0140             '/^[\x{9}\x{A}\x{D}\x{20}-\x{7E}\x{A0}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]*$/Du',
0141             $str
0142         )) {
0143             return $str;
0144         }
0145 
0146         $mState = 0; // cached expected number of octets after the current octet
0147                      // until the beginning of the next UTF8 character sequence
0148         $mUcs4  = 0; // cached Unicode character
0149         $mBytes = 1; // cached expected number of octets in the current sequence
0150 
0151         // original code involved an $out that was an array of Unicode
0152         // codepoints.  Instead of having to convert back into UTF-8, we've
0153         // decided to directly append valid UTF-8 characters onto a string
0154         // $out once they're done.  $char accumulates raw bytes, while $mUcs4
0155         // turns into the Unicode code point, so there's some redundancy.
0156 
0157         $out = '';
0158         $char = '';
0159 
0160         $len = strlen($str);
0161         for ($i = 0; $i < $len; $i++) {
0162             $in = ord($str{$i});
0163             $char .= $str[$i]; // append byte to char
0164             if (0 == $mState) {
0165                 // When mState is zero we expect either a US-ASCII character
0166                 // or a multi-octet sequence.
0167                 if (0 == (0x80 & ($in))) {
0168                     // US-ASCII, pass straight through.
0169                     if (($in <= 31 || $in == 127) &&
0170                         !($in == 9 || $in == 13 || $in == 10) // save \r\t\n
0171                     ) {
0172                         // control characters, remove
0173                     } else {
0174                         $out .= $char;
0175                     }
0176                     // reset
0177                     $char = '';
0178                     $mBytes = 1;
0179                 } elseif (0xC0 == (0xE0 & ($in))) {
0180                     // First octet of 2 octet sequence
0181                     $mUcs4 = ($in);
0182                     $mUcs4 = ($mUcs4 & 0x1F) << 6;
0183                     $mState = 1;
0184                     $mBytes = 2;
0185                 } elseif (0xE0 == (0xF0 & ($in))) {
0186                     // First octet of 3 octet sequence
0187                     $mUcs4 = ($in);
0188                     $mUcs4 = ($mUcs4 & 0x0F) << 12;
0189                     $mState = 2;
0190                     $mBytes = 3;
0191                 } elseif (0xF0 == (0xF8 & ($in))) {
0192                     // First octet of 4 octet sequence
0193                     $mUcs4 = ($in);
0194                     $mUcs4 = ($mUcs4 & 0x07) << 18;
0195                     $mState = 3;
0196                     $mBytes = 4;
0197                 } elseif (0xF8 == (0xFC & ($in))) {
0198                     // First octet of 5 octet sequence.
0199                     //
0200                     // This is illegal because the encoded codepoint must be
0201                     // either:
0202                     // (a) not the shortest form or
0203                     // (b) outside the Unicode range of 0-0x10FFFF.
0204                     // Rather than trying to resynchronize, we will carry on
0205                     // until the end of the sequence and let the later error
0206                     // handling code catch it.
0207                     $mUcs4 = ($in);
0208                     $mUcs4 = ($mUcs4 & 0x03) << 24;
0209                     $mState = 4;
0210                     $mBytes = 5;
0211                 } elseif (0xFC == (0xFE & ($in))) {
0212                     // First octet of 6 octet sequence, see comments for 5
0213                     // octet sequence.
0214                     $mUcs4 = ($in);
0215                     $mUcs4 = ($mUcs4 & 1) << 30;
0216                     $mState = 5;
0217                     $mBytes = 6;
0218                 } else {
0219                     // Current octet is neither in the US-ASCII range nor a
0220                     // legal first octet of a multi-octet sequence.
0221                     $mState = 0;
0222                     $mUcs4  = 0;
0223                     $mBytes = 1;
0224                     $char = '';
0225                 }
0226             } else {
0227                 // When mState is non-zero, we expect a continuation of the
0228                 // multi-octet sequence
0229                 if (0x80 == (0xC0 & ($in))) {
0230                     // Legal continuation.
0231                     $shift = ($mState - 1) * 6;
0232                     $tmp = $in;
0233                     $tmp = ($tmp & 0x0000003F) << $shift;
0234                     $mUcs4 |= $tmp;
0235 
0236                     if (0 == --$mState) {
0237                         // End of the multi-octet sequence. mUcs4 now contains
0238                         // the final Unicode codepoint to be output
0239 
0240                         // Check for illegal sequences and codepoints.
0241 
0242                         // From Unicode 3.1, non-shortest form is illegal
0243                         if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
0244                             ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
0245                             ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
0246                             (4 < $mBytes) ||
0247                             // From Unicode 3.2, surrogate characters = illegal
0248                             (($mUcs4 & 0xFFFFF800) == 0xD800) ||
0249                             // Codepoints outside the Unicode range are illegal
0250                             ($mUcs4 > 0x10FFFF)
0251                         ) {
0252 
0253                         } elseif (0xFEFF != $mUcs4 && // omit BOM
0254                             // check for valid Char unicode codepoints
0255                             (
0256                                 0x9 == $mUcs4 ||
0257                                 0xA == $mUcs4 ||
0258                                 0xD == $mUcs4 ||
0259                                 (0x20 <= $mUcs4 && 0x7E >= $mUcs4) ||
0260                                 // 7F-9F is not strictly prohibited by XML,
0261                                 // but it is non-SGML, and thus we don't allow it
0262                                 (0xA0 <= $mUcs4 && 0xD7FF >= $mUcs4) ||
0263                                 (0xE000 <= $mUcs4 && 0xFFFD >= $mUcs4) ||
0264                                 (0x10000 <= $mUcs4 && 0x10FFFF >= $mUcs4)
0265                             )
0266                         ) {
0267                             $out .= $char;
0268                         }
0269                         // initialize UTF8 cache (reset)
0270                         $mState = 0;
0271                         $mUcs4  = 0;
0272                         $mBytes = 1;
0273                         $char = '';
0274                     }
0275                 } else {
0276                     // ((0xC0 & (*in) != 0x80) && (mState != 0))
0277                     // Incomplete multi-octet sequence.
0278                     // used to result in complete fail, but we'll reset
0279                     $mState = 0;
0280                     $mUcs4  = 0;
0281                     $mBytes = 1;
0282                     $char ='';
0283                 }
0284             }
0285         }
0286         return $out;
0287     }
0288 
0289     /**
0290      * Translates a Unicode codepoint into its corresponding UTF-8 character.
0291      * @note Based on Feyd's function at
0292      *       <http://forums.devnetwork.net/viewtopic.php?p=191404#191404>,
0293      *       which is in public domain.
0294      * @note While we're going to do code point parsing anyway, a good
0295      *       optimization would be to refuse to translate code points that
0296      *       are non-SGML characters.  However, this could lead to duplication.
0297      * @note This is very similar to the unichr function in
0298      *       maintenance/generate-entity-file.php (although this is superior,
0299      *       due to its sanity checks).
0300      */
0301 
0302     // +----------+----------+----------+----------+
0303     // | 33222222 | 22221111 | 111111   |          |
0304     // | 10987654 | 32109876 | 54321098 | 76543210 | bit
0305     // +----------+----------+----------+----------+
0306     // |          |          |          | 0xxxxxxx | 1 byte 0x00000000..0x0000007F
0307     // |          |          | 110yyyyy | 10xxxxxx | 2 byte 0x00000080..0x000007FF
0308     // |          | 1110zzzz | 10yyyyyy | 10xxxxxx | 3 byte 0x00000800..0x0000FFFF
0309     // | 11110www | 10wwzzzz | 10yyyyyy | 10xxxxxx | 4 byte 0x00010000..0x0010FFFF
0310     // +----------+----------+----------+----------+
0311     // | 00000000 | 00011111 | 11111111 | 11111111 | Theoretical upper limit of legal scalars: 2097151 (0x001FFFFF)
0312     // | 00000000 | 00010000 | 11111111 | 11111111 | Defined upper limit of legal scalar codes
0313     // +----------+----------+----------+----------+
0314 
0315     public static function unichr($code)
0316     {
0317         if ($code > 1114111 or $code < 0 or
0318           ($code >= 55296 and $code <= 57343) ) {
0319             // bits are set outside the "valid" range as defined
0320             // by UNICODE 4.1.0
0321             return '';
0322         }
0323 
0324         $x = $y = $z = $w = 0;
0325         if ($code < 128) {
0326             // regular ASCII character
0327             $x = $code;
0328         } else {
0329             // set up bits for UTF-8
0330             $x = ($code & 63) | 128;
0331             if ($code < 2048) {
0332                 $y = (($code & 2047) >> 6) | 192;
0333             } else {
0334                 $y = (($code & 4032) >> 6) | 128;
0335                 if ($code < 65536) {
0336                     $z = (($code >> 12) & 15) | 224;
0337                 } else {
0338                     $z = (($code >> 12) & 63) | 128;
0339                     $w = (($code >> 18) & 7)  | 240;
0340                 }
0341             }
0342         }
0343         // set up the actual character
0344         $ret = '';
0345         if ($w) {
0346             $ret .= chr($w);
0347         }
0348         if ($z) {
0349             $ret .= chr($z);
0350         }
0351         if ($y) {
0352             $ret .= chr($y);
0353         }
0354         $ret .= chr($x);
0355 
0356         return $ret;
0357     }
0358 
0359     /**
0360      * @return bool
0361      */
0362     public static function iconvAvailable()
0363     {
0364         static $iconv = null;
0365         if ($iconv === null) {
0366             $iconv = function_exists('iconv') && self::testIconvTruncateBug() != self::ICONV_UNUSABLE;
0367         }
0368         return $iconv;
0369     }
0370 
0371     /**
0372      * Convert a string to UTF-8 based on configuration.
0373      * @param string $str The string to convert
0374      * @param HTMLPurifier_Config $config
0375      * @param HTMLPurifier_Context $context
0376      * @return string
0377      */
0378     public static function convertToUTF8($str, $config, $context)
0379     {
0380         $encoding = $config->get('Core.Encoding');
0381         if ($encoding === 'utf-8') {
0382             return $str;
0383         }
0384         static $iconv = null;
0385         if ($iconv === null) {
0386             $iconv = self::iconvAvailable();
0387         }
0388         if ($iconv && !$config->get('Test.ForceNoIconv')) {
0389             // unaffected by bugs, since UTF-8 support all characters
0390             $str = self::unsafeIconv($encoding, 'utf-8//IGNORE', $str);
0391             if ($str === false) {
0392                 // $encoding is not a valid encoding
0393                 trigger_error('Invalid encoding ' . $encoding, E_USER_ERROR);
0394                 return '';
0395             }
0396             // If the string is bjorked by Shift_JIS or a similar encoding
0397             // that doesn't support all of ASCII, convert the naughty
0398             // characters to their true byte-wise ASCII/UTF-8 equivalents.
0399             $str = strtr($str, self::testEncodingSupportsASCII($encoding));
0400             return $str;
0401         } elseif ($encoding === 'iso-8859-1') {
0402             $str = utf8_encode($str);
0403             return $str;
0404         }
0405         $bug = HTMLPurifier_Encoder::testIconvTruncateBug();
0406         if ($bug == self::ICONV_OK) {
0407             trigger_error('Encoding not supported, please install iconv', E_USER_ERROR);
0408         } else {
0409             trigger_error(
0410                 'You have a buggy version of iconv, see https://bugs.php.net/bug.php?id=48147 ' .
0411                 'and http://sourceware.org/bugzilla/show_bug.cgi?id=13541',
0412                 E_USER_ERROR
0413             );
0414         }
0415     }
0416 
0417     /**
0418      * Converts a string from UTF-8 based on configuration.
0419      * @param string $str The string to convert
0420      * @param HTMLPurifier_Config $config
0421      * @param HTMLPurifier_Context $context
0422      * @return string
0423      * @note Currently, this is a lossy conversion, with unexpressable
0424      *       characters being omitted.
0425      */
0426     public static function convertFromUTF8($str, $config, $context)
0427     {
0428         $encoding = $config->get('Core.Encoding');
0429         if ($escape = $config->get('Core.EscapeNonASCIICharacters')) {
0430             $str = self::convertToASCIIDumbLossless($str);
0431         }
0432         if ($encoding === 'utf-8') {
0433             return $str;
0434         }
0435         static $iconv = null;
0436         if ($iconv === null) {
0437             $iconv = self::iconvAvailable();
0438         }
0439         if ($iconv && !$config->get('Test.ForceNoIconv')) {
0440             // Undo our previous fix in convertToUTF8, otherwise iconv will barf
0441             $ascii_fix = self::testEncodingSupportsASCII($encoding);
0442             if (!$escape && !empty($ascii_fix)) {
0443                 $clear_fix = array();
0444                 foreach ($ascii_fix as $utf8 => $native) {
0445                     $clear_fix[$utf8] = '';
0446                 }
0447                 $str = strtr($str, $clear_fix);
0448             }
0449             $str = strtr($str, array_flip($ascii_fix));
0450             // Normal stuff
0451             $str = self::iconv('utf-8', $encoding . '//IGNORE', $str);
0452             return $str;
0453         } elseif ($encoding === 'iso-8859-1') {
0454             $str = utf8_decode($str);
0455             return $str;
0456         }
0457         trigger_error('Encoding not supported', E_USER_ERROR);
0458         // You might be tempted to assume that the ASCII representation
0459         // might be OK, however, this is *not* universally true over all
0460         // encodings.  So we take the conservative route here, rather
0461         // than forcibly turn on %Core.EscapeNonASCIICharacters
0462     }
0463 
0464     /**
0465      * Lossless (character-wise) conversion of HTML to ASCII
0466      * @param string $str UTF-8 string to be converted to ASCII
0467      * @return string ASCII encoded string with non-ASCII character entity-ized
0468      * @warning Adapted from MediaWiki, claiming fair use: this is a common
0469      *       algorithm. If you disagree with this license fudgery,
0470      *       implement it yourself.
0471      * @note Uses decimal numeric entities since they are best supported.
0472      * @note This is a DUMB function: it has no concept of keeping
0473      *       character entities that the projected character encoding
0474      *       can allow. We could possibly implement a smart version
0475      *       but that would require it to also know which Unicode
0476      *       codepoints the charset supported (not an easy task).
0477      * @note Sort of with cleanUTF8() but it assumes that $str is
0478      *       well-formed UTF-8
0479      */
0480     public static function convertToASCIIDumbLossless($str)
0481     {
0482         $bytesleft = 0;
0483         $result = '';
0484         $working = 0;
0485         $len = strlen($str);
0486         for ($i = 0; $i < $len; $i++) {
0487             $bytevalue = ord($str[$i]);
0488             if ($bytevalue <= 0x7F) { //0xxx xxxx
0489                 $result .= chr($bytevalue);
0490                 $bytesleft = 0;
0491             } elseif ($bytevalue <= 0xBF) { //10xx xxxx
0492                 $working = $working << 6;
0493                 $working += ($bytevalue & 0x3F);
0494                 $bytesleft--;
0495                 if ($bytesleft <= 0) {
0496                     $result .= "&#" . $working . ";";
0497                 }
0498             } elseif ($bytevalue <= 0xDF) { //110x xxxx
0499                 $working = $bytevalue & 0x1F;
0500                 $bytesleft = 1;
0501             } elseif ($bytevalue <= 0xEF) { //1110 xxxx
0502                 $working = $bytevalue & 0x0F;
0503                 $bytesleft = 2;
0504             } else { //1111 0xxx
0505                 $working = $bytevalue & 0x07;
0506                 $bytesleft = 3;
0507             }
0508         }
0509         return $result;
0510     }
0511 
0512     /** No bugs detected in iconv. */
0513     const ICONV_OK = 0;
0514 
0515     /** Iconv truncates output if converting from UTF-8 to another
0516      *  character set with //IGNORE, and a non-encodable character is found */
0517     const ICONV_TRUNCATES = 1;
0518 
0519     /** Iconv does not support //IGNORE, making it unusable for
0520      *  transcoding purposes */
0521     const ICONV_UNUSABLE = 2;
0522 
0523     /**
0524      * glibc iconv has a known bug where it doesn't handle the magic
0525      * //IGNORE stanza correctly.  In particular, rather than ignore
0526      * characters, it will return an EILSEQ after consuming some number
0527      * of characters, and expect you to restart iconv as if it were
0528      * an E2BIG.  Old versions of PHP did not respect the errno, and
0529      * returned the fragment, so as a result you would see iconv
0530      * mysteriously truncating output. We can work around this by
0531      * manually chopping our input into segments of about 8000
0532      * characters, as long as PHP ignores the error code.  If PHP starts
0533      * paying attention to the error code, iconv becomes unusable.
0534      *
0535      * @return int Error code indicating severity of bug.
0536      */
0537     public static function testIconvTruncateBug()
0538     {
0539         static $code = null;
0540         if ($code === null) {
0541             // better not use iconv, otherwise infinite loop!
0542             $r = self::unsafeIconv('utf-8', 'ascii//IGNORE', "\xCE\xB1" . str_repeat('a', 9000));
0543             if ($r === false) {
0544                 $code = self::ICONV_UNUSABLE;
0545             } elseif (($c = strlen($r)) < 9000) {
0546                 $code = self::ICONV_TRUNCATES;
0547             } elseif ($c > 9000) {
0548                 trigger_error(
0549                     'Your copy of iconv is extremely buggy. Please notify HTML Purifier maintainers: ' .
0550                     'include your iconv version as per phpversion()',
0551                     E_USER_ERROR
0552                 );
0553             } else {
0554                 $code = self::ICONV_OK;
0555             }
0556         }
0557         return $code;
0558     }
0559 
0560     /**
0561      * This expensive function tests whether or not a given character
0562      * encoding supports ASCII. 7/8-bit encodings like Shift_JIS will
0563      * fail this test, and require special processing. Variable width
0564      * encodings shouldn't ever fail.
0565      *
0566      * @param string $encoding Encoding name to test, as per iconv format
0567      * @param bool $bypass Whether or not to bypass the precompiled arrays.
0568      * @return Array of UTF-8 characters to their corresponding ASCII,
0569      *      which can be used to "undo" any overzealous iconv action.
0570      */
0571     public static function testEncodingSupportsASCII($encoding, $bypass = false)
0572     {
0573         // All calls to iconv here are unsafe, proof by case analysis:
0574         // If ICONV_OK, no difference.
0575         // If ICONV_TRUNCATE, all calls involve one character inputs,
0576         // so bug is not triggered.
0577         // If ICONV_UNUSABLE, this call is irrelevant
0578         static $encodings = array();
0579         if (!$bypass) {
0580             if (isset($encodings[$encoding])) {
0581                 return $encodings[$encoding];
0582             }
0583             $lenc = strtolower($encoding);
0584             switch ($lenc) {
0585                 case 'shift_jis':
0586                     return array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~');
0587                 case 'johab':
0588                     return array("\xE2\x82\xA9" => '\\');
0589             }
0590             if (strpos($lenc, 'iso-8859-') === 0) {
0591                 return array();
0592             }
0593         }
0594         $ret = array();
0595         if (self::unsafeIconv('UTF-8', $encoding, 'a') === false) {
0596             return false;
0597         }
0598         for ($i = 0x20; $i <= 0x7E; $i++) { // all printable ASCII chars
0599             $c = chr($i); // UTF-8 char
0600             $r = self::unsafeIconv('UTF-8', "$encoding//IGNORE", $c); // initial conversion
0601             if ($r === '' ||
0602                 // This line is needed for iconv implementations that do not
0603                 // omit characters that do not exist in the target character set
0604                 ($r === $c && self::unsafeIconv($encoding, 'UTF-8//IGNORE', $r) !== $c)
0605             ) {
0606                 // Reverse engineer: what's the UTF-8 equiv of this byte
0607                 // sequence? This assumes that there's no variable width
0608                 // encoding that doesn't support ASCII.
0609                 $ret[self::unsafeIconv($encoding, 'UTF-8//IGNORE', $c)] = $c;
0610             }
0611         }
0612         $encodings[$encoding] = $ret;
0613         return $ret;
0614     }
0615 }
0616 
0617 // vim: et sw=4 sts=4