2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
7 * PHP-P10 is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with PHP-P10; if not, write to the Free Software Foundation, *
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 ************************************************************************
23 * Uplink/Numerics.class.php
25 * P10 numeric functions
27 ************************************************************************
28 * accessable functions
30 * static String intToNum(int $int, int $length)
31 * returns the numeric representing $int
33 * static int numToInt(String $numeric)
34 * returns the integer value, the numeric represents
36 * static String parseIP(String $numeric)
37 * parses an IP Address in numeric format
39 * static String numericFromIP(String $ip)
40 * builds a numeric representing the IP
44 private static $base64chars = array(
45 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
46 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
47 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
48 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
50 private static $base64charsLength = 64;
52 public static function intToNum($int, $length) {
54 for($pos = $length-1; $pos >= 0; $pos--) {
55 //current position represents floor($int / ($base64charsLength ^ $pos))
57 for($i = 0; $i < $pos; $i++) {
58 $base = $base * self::$base64charsLength;
60 $posValue = floor($int / $base);
61 $int -= $posValue * $base;
62 //get the char representing $posValue
63 $posChar = self::$base64chars[$posValue];
70 public static function numToInt($numeric) {
73 for($pos = strlen($numeric)-1; $pos >= 0; $pos--) {
74 $posValue = array_search($numeric[$pos], self::$base64chars);
75 $int = ($posValue * $base);
76 $base = $base * self::$base64charsLength;