punycode.php

Non-length-limited punycode en-/decoder
git clone git://git.finwo.net/lib/punycode.php
Log | Files | Refs | README | LICENSE

commit 54734329dd693a7afd3741e790c91bc1732b45af
Author: finwo <finwo@pm.me>
Date:   Thu,  2 Mar 2017 17:31:15 +0100

Project init

Diffstat:
A.gitignore | 3+++
A.travis.yml | 11+++++++++++
ALICENSE.md | 36++++++++++++++++++++++++++++++++++++
AREADME.md | 27+++++++++++++++++++++++++++
Acomposer.json | 18++++++++++++++++++
Acomposer.lock | 19+++++++++++++++++++
Aphpunit.xml | 19+++++++++++++++++++
Asrc/Punycode.php | 307+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/LintTest.php | 33+++++++++++++++++++++++++++++++++
9 files changed, 473 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +.idea +.zedstate diff --git a/.travis.yml b/.travis.yml @@ -0,0 +1,11 @@ +language: php +php: + - '5.3' + - '5.4' + - '5.5' + - '5.6' + - '7.0' + +install: +# - yes '' | pecl install -f memcached + - composer update --prefer-dist diff --git a/LICENSE.md b/LICENSE.md @@ -0,0 +1,36 @@ +The project is heavily based on [true/php-punycode](https://github.com/true/php-punycode), so their license is included. + +## Finwo + +MIT License + +Copyright (c) 2017 Finwo + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +## TrueServer + +Copyright (c) 2014 TrueServer B.V. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md @@ -0,0 +1,27 @@ +Finwo / Punycode +================ +Non-length-limited punycode en-/decoder + +## Usage + +Encoding a string + +```php +$encodedString = Punycode::encode( $stringToEncode ); +``` + +Decoding a string + +```php +$decodedString = Punycode::decode( $encodedString ); +``` + +Checking if a string is valid punycode + +```php +$isValid = Punycode::isPunycode( $stringToTest ); +``` + +## Contributing + +After checking the [Github issues](https://github.com/finwo/php-punycode/issues) and confirming that your request isn't already being worked on, feel free to spawn a new fork and send in a pull request. diff --git a/composer.json b/composer.json @@ -0,0 +1,18 @@ +{ + "name" : "finwo/punycode", + "minimum-stability": "stable", + "authors" : [ + { + "name" : "Robin Bron", + "email": "robin@finwo.nl" + } + ], + "autoload" : { + "psr-4": { + "Finwo\\Punycode\\": "src" + } + }, + "require" : { + "php": ">=5.3" + } +} diff --git a/composer.lock b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "28f20e0565eff24f78166060033f91cf", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3" + }, + "platform-dev": [] +} diff --git a/phpunit.xml b/phpunit.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit bootstrap="vendor/autoload.php" colors="true"> + + <testsuites> + <testsuite name="finwo/punycode test suite"> + <directory>./tests/</directory> + </testsuite> + </testsuites> + + <filter> + <whitelist> + <directory>./</directory> + <exclude> + <directory>./tests</directory> + </exclude> + </whitelist> + </filter> +</phpunit> diff --git a/src/Punycode.php b/src/Punycode.php @@ -0,0 +1,307 @@ +<?php + +namespace Finwo\Punycode; + +/** + * Class Punycode + * + * Fully static Punycode en-/decoder based on https://www.ietf.org/rfc/rfc3492.txt + * This encoder does not limit string sizes, like https://github.com/true/php-punycode does + * + * @package Finwo\Punycode + */ +class Punycode +{ + /** + * Bootstring parameter values + * + */ + const BASE = 36; + const DAMP = 700; + const DELIMITER = '-'; + const INITIAL_BIAS = 72; + const INITIAL_N = 128; + const PREFIX = 'xn--'; + const SKEW = 38; + const TMAX = 26; + const TMIN = 1; + + /** + * See page 9 of the RFC + * + * @var array + */ + protected static $decodeTable = array( + 'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5, + 'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11, + 'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17, + 's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23, + 'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29, + '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35, + ); + + /** + * This will be build during __construct + * + * @var array + */ + protected static $encodeTable = array(); + + /** + * @return array + */ + protected static function getDecodeTable() + { + return self::$decodeTable; + } + + /** + * @return array + */ + protected static function getEncodeTable() + { + if(!count(self::$encodeTable)) { + self::$encodeTable = array_keys(self::$decodeTable); + } + return self::$encodeTable; + } + + /** + * List code points for a given input + * + * @param string $input + * @return array Multi-dimension array with basic, non-basic and aggregated code points + */ + protected static function listCodePoints($input) + { + $codePoints = array( + 'all' => array(), + 'basic' => array(), + 'nonBasic' => array(), + ); + + $length = mb_strlen($input); + for ($i = 0; $i < $length; $i++) { + $char = mb_substr($input, $i, 1); + $code = self::charToCodePoint($char); + if ($code < 128) { + $codePoints['all'][] = $codePoints['basic'][] = $code; + } else { + $codePoints['all'][] = $codePoints['nonBasic'][] = $code; + } + } + + return $codePoints; + } + + /** + * Convert a single or multi-byte character to its code point + * + * @param string $char + * @return integer + */ + protected static function charToCodePoint($char) + { + $code = ord($char[0]); + if ($code < 128) { + return $code; + } elseif ($code < 224) { + return (($code - 192) * 64) + (ord($char[1]) - 128); + } elseif ($code < 240) { + return (($code - 224) * 4096) + ((ord($char[1]) - 128) * 64) + (ord($char[2]) - 128); + } else { + return (($code - 240) * 262144) + ((ord($char[1]) - 128) * 4096) + ((ord($char[2]) - 128) * 64) + (ord($char[3]) - 128); + } + } + + /** + * Convert a code point to its single or multi-byte character + * + * @param integer $code + * @return string + */ + protected static function codePointToChar($code) + { + if ($code <= 0x7F) { + return chr($code); + } elseif ($code <= 0x7FF) { + return chr(($code >> 6) + 192) . chr(($code & 63) + 128); + } elseif ($code <= 0xFFFF) { + return chr(($code >> 12) + 224) . chr((($code >> 6) & 63) + 128) . chr(($code & 63) + 128); + } else { + return chr(($code >> 18) + 240) . chr((($code >> 12) & 63) + 128) . chr((($code >> 6) & 63) + 128) . chr(($code & 63) + 128); + } + } + + /** + * Calculate the bias threshold to fall between TMIN and TMAX + * + * @param integer $k + * @param integer $bias + * @return integer + */ + protected static function calculateThreshold($k, $bias) + { + if ($k <= $bias + static::TMIN) { + return static::TMIN; + } elseif ($k >= $bias + static::TMAX) { + return static::TMAX; + } + return $k - $bias; + } + + /** + * Bias adaptation + * + * @param integer $delta + * @param integer $numPoints + * @param boolean $firstTime + * @return integer + */ + protected static function adapt($delta, $numPoints, $firstTime) + { + $delta = (int) ( + ($firstTime) + ? $delta / static::DAMP + : $delta / 2 + ); + $delta += (int) ($delta / $numPoints); + + $k = 0; + while ($delta > ((static::BASE - static::TMIN) * static::TMAX) / 2) { + $delta = (int) ($delta / (static::BASE - static::TMIN)); + $k = $k + static::BASE; + } + $k = $k + (int) (((static::BASE - static::TMIN + 1) * $delta) / ($delta + static::SKEW)); + + return $k; + } + + /** + * @param string $input + * + * @return string $encodedString + */ + public static function encode( $input ) + { + $codePoints = self::listCodePoints($input); + + $n = static::INITIAL_N; + $bias = static::INITIAL_BIAS; + $delta = 0; + $h = $b = count($codePoints['basic']); + + $output = ''; + foreach ($codePoints['basic'] as $code) { + $output .= self::codePointToChar($code); + } + if ($input === $output) { + return $output; + } + if ($b > 0) { + $output .= static::DELIMITER; + } + + $codePoints['nonBasic'] = array_unique($codePoints['nonBasic']); + sort($codePoints['nonBasic']); + + $i = 0; + $length = mb_strlen($input); + while ($h < $length) { + $m = $codePoints['nonBasic'][$i++]; + $delta = $delta + ($m - $n) * ($h + 1); + $n = $m; + + foreach ($codePoints['all'] as $c) { + if ($c < $n || $c < static::INITIAL_N) { + $delta++; + } + if ($c === $n) { + $q = $delta; + for ($k = static::BASE;; $k += static::BASE) { + $t = self::calculateThreshold($k, $bias); + if ($q < $t) { + break; + } + + $code = $t + (($q - $t) % (static::BASE - $t)); + $output .= static::$encodeTable[$code]; + + $q = ($q - $t) / (static::BASE - $t); + } + + $output .= static::$encodeTable[$q]; + $bias = self::adapt($delta, $h + 1, ($h === $b)); + $delta = 0; + $h++; + } + } + + $delta++; + $n++; + } + $out = static::PREFIX . $output; + + return $out; + } + + /** + * @param string $encodedString + * + * @return string $decodedString + */ + public static function decode( $encodedString ) + { + $n = static::INITIAL_N; + $i = 0; + $bias = static::INITIAL_BIAS; + $output = ''; + + $pos = strrpos($encodedString, static::DELIMITER); + if ($pos !== false) { + $output = substr($encodedString, 0, $pos++); + } else { + $pos = 0; + } + + $outputLength = strlen($output); + $inputLength = strlen($encodedString); + while ($pos < $inputLength) { + $oldi = $i; + $w = 1; + + for ($k = static::BASE;; $k += static::BASE) { + $digit = static::$decodeTable[$encodedString[$pos++]]; + $i = $i + ($digit * $w); + $t = self::calculateThreshold($k, $bias); + + if ($digit < $t) { + break; + } + + $w = $w * (static::BASE - $t); + } + + $bias = self::adapt($i - $oldi, ++$outputLength, ($oldi === 0)); + $n = $n + (int) ($i / $outputLength); + $i = $i % ($outputLength); + $output = mb_substr($output, 0, $i) . self::codePointToChar($n) . mb_substr($output, $i, $outputLength - 1); + + $i++; + } + + return $output; + } + + /** + * @param string $stringToCheck + * + * @return bool + */ + public static function isPunycode( $stringToCheck ) + { + if(substr($stringToCheck, 0, strlen(static::PREFIX)) != static::PREFIX) return false; + if(strpos($stringToCheck, static::DELIMITER, strlen(static::PREFIX)) === false) return false; + return true; + } +} diff --git a/tests/LintTest.php b/tests/LintTest.php @@ -0,0 +1,33 @@ +<?php + +class LintTest extends \PHPUnit_Framework_TestCase +{ + public function testSrc() + { + // Main entry + $fileList = \glob('src/'); + + // Loop through known files + while ( $inode = \array_shift($fileList) ) { + + // Try to iterate deeper + if ( is_dir($inode) ) { + $fileList = \array_merge($fileList, \glob(\realpath($inode).'/*')); + continue; + } + + // If we're a PHP file + if (\preg_match('/^.+\.(php|inc)$/i', $inode)) { + // Run a unit test + $this->lintFile($inode); + } + + } + } + + private function lintFile($filename = '') + { + // And actually run the test (with proper error message) + $this->assertContains('No syntax errors', \exec(\sprintf('php -l "%s"', $filename), $out), \sprintf("%s contains syntax errors", $filename)); + } +}