Advertisement
Guest User

Untitled

a guest
May 28th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.68 KB | None | 0 0
  1. <?php
  2.  
  3. function benc($str) //bencoding
  4. {
  5.   if (is_string($str)) { //string
  6.     return strlen($str) . ':' . $str;
  7.   }
  8.  
  9.   if (is_numeric($str)) { //integer
  10.     return 'i' . $str . 'e';
  11.   }
  12.  
  13.   if (is_array($str)) {
  14.     $ret_str = ''; //the return string
  15.    
  16.     $k = key($str); //we check the 1st key, if the key is 0 then is a list if not a dictionary
  17.     foreach($str as $var => $val) {
  18.       if ($k) { //is dictionary
  19.         $ret_str .= benc($var); //bencode the var
  20.       }
  21.       $ret_str .= benc($val); //we recursivly bencode the contents
  22.     }
  23.    
  24.     if ($k) { //is dictionary
  25.       return 'd' . $ret_str . 'e';
  26.     }
  27.    
  28.     return 'l' . $ret_str . 'e';
  29.   }
  30. }
  31.  
  32. function bdec_file($f, $ms)
  33. {
  34.     $fp = fopen($f, "rb");
  35.     if (!$fp)
  36.         return;
  37.     $e = fread($fp, $ms);
  38.     fclose($fp);
  39.     return bdec($e);
  40. }
  41.  
  42. function bdec($str, &$_len = 0) //bdecoding
  43. {
  44.   $type = substr($str, 0, 1);
  45.  
  46.   if (is_numeric($type)) {
  47.     $type = 's';
  48.   }
  49.  
  50.   switch ($type) {
  51.     case 'i': //integer
  52.       $p = strpos($str, 'e');
  53.       $_len = $p + 1; //lenght of bencoded data
  54.       return intval(substr($str, 1, $p - 1));
  55.     break;
  56.  
  57.     case 's': //string
  58.       $p = strpos($str, ':');
  59.       $len = substr($str, 0, $p);
  60.       $_len = $len + $p + 1; //lenght of bencoded data
  61.       return substr($str, $p + 1, $len);
  62.     break;
  63.    
  64.     case 'l': //list
  65.       $l = 1;
  66.       $ret_array = array();
  67.       while (substr($str, $l, 1) != 'e') {
  68.         $ret_array[] = bdec(substr($str, $l), $len);
  69.         $l += $len;
  70.       }
  71.       $_len = $l + 1; //lenght of bencoded data
  72.       return $ret_array;
  73.     break;
  74.    
  75.     case 'd': //dictionary
  76.       $l = 1;
  77.       $ret_array = array();
  78.       while (substr($str, $l, 1) != 'e') {
  79.         $var = bdec(substr($str, $l), $len);
  80.         $l += $len;
  81.        
  82.         $ret_array[$var] = bdec(substr($str, $l), $len);
  83.         $l += $len;
  84.       }
  85.       $_len = $l + 1; //lenght of bencoded data
  86.       return $ret_array;
  87.     break;
  88.   }
  89. }
  90.  
  91. function hex2bin($hex){
  92.     $bin="";
  93.     for($i=0;$i<strlen($hex);$i+=2) {
  94.         $bin .= chr(hexdec(substr($hex,$i,2)));
  95.     }
  96.     return $bin;
  97. }
  98.  
  99. //
  100. // +----------------------------------------------------------------------+
  101. // | Base32 Library                                                     |
  102. // +----------------------------------------------------------------------+
  103. // | Copyright (c) 2001 The PHP Group                                     |
  104. // +----------------------------------------------------------------------+
  105. // | This source file is dual-licensed. It is available under the terms   |
  106. // | of the GNU GPL v2.0 and under the terms of the PHP license version   |
  107. // | 2.02,  available at through the world-wide-web at                    |
  108. // | available at through the world-wide-web at                           |
  109. // | http://www.php.net/license/2_02.txt.                                 |
  110. // +----------------------------------------------------------------------+
  111. // |  Minor fixes and additional functions by Allan Hansen.               |
  112. // |  Moodle porting work by Martin Langhoff                              |
  113. // +----------------------------------------------------------------------+
  114. // | base32.php - based on race.php  - RACE encode and decode strings.    |
  115. // +----------------------------------------------------------------------+
  116. // | Authors: Allan Hansen  <All@nHansen.dk>                              |
  117. // |          Arjan Wekking <a.wekking@synantics.nl>                      |
  118. // |          Martin Langhoff <martin@catalyst.net.nz>                    |
  119. // +----------------------------------------------------------------------+
  120. //
  121.  
  122. /**
  123.  * Base32 encode a binary string
  124.  *
  125.  * @param    $inString   Binary string to base32 encode
  126.  *
  127.  * @return   $outString  Base32 encoded $inString
  128.  *
  129.  * @access   private
  130.  *
  131.  */
  132.  
  133. function base32_encode ($inString)
  134. {
  135.     $outString = "";
  136.     $compBits = "";
  137.     $BASE32_TABLE = array(
  138.                           '00000' => 0x61,
  139.                           '00001' => 0x62,
  140.                           '00010' => 0x63,
  141.                           '00011' => 0x64,
  142.                           '00100' => 0x65,
  143.                           '00101' => 0x66,
  144.                           '00110' => 0x67,
  145.                           '00111' => 0x68,
  146.                           '01000' => 0x69,
  147.                           '01001' => 0x6a,
  148.                           '01010' => 0x6b,
  149.                           '01011' => 0x6c,
  150.                           '01100' => 0x6d,
  151.                           '01101' => 0x6e,
  152.                           '01110' => 0x6f,
  153.                           '01111' => 0x70,
  154.                           '10000' => 0x71,
  155.                           '10001' => 0x72,
  156.                           '10010' => 0x73,
  157.                           '10011' => 0x74,
  158.                           '10100' => 0x75,
  159.                           '10101' => 0x76,
  160.                           '10110' => 0x77,
  161.                           '10111' => 0x78,
  162.                           '11000' => 0x79,
  163.                           '11001' => 0x7a,
  164.                           '11010' => 0x32,
  165.                           '11011' => 0x33,
  166.                           '11100' => 0x34,
  167.                           '11101' => 0x35,
  168.                           '11110' => 0x36,
  169.                           '11111' => 0x37,
  170.                           );
  171.    
  172.     /* Turn the compressed string into a string that represents the bits as 0 and 1. */
  173.     for ($i = 0; $i < strlen($inString); $i++) {
  174.         $compBits .= str_pad(decbin(ord(substr($inString,$i,1))), 8, '0', STR_PAD_LEFT);
  175.     }
  176.    
  177.     /* Pad the value with enough 0's to make it a multiple of 5 */
  178.     if((strlen($compBits) % 5) != 0) {
  179.         $compBits = str_pad($compBits, strlen($compBits)+(5-(strlen($compBits)%5)), '0', STR_PAD_RIGHT);
  180.     }
  181.    
  182.     /* Create an array by chunking it every 5 chars */
  183.     $fiveBitsArray = split("\n",rtrim(chunk_split($compBits, 5, "\n")));
  184.    
  185.     /* Look-up each chunk and add it to $outstring */
  186.     foreach($fiveBitsArray as $fiveBitsString) {
  187.         $outString .= chr($BASE32_TABLE[$fiveBitsString]);
  188.     }
  189.    
  190.     return $outString;
  191. }
  192.  
  193.  
  194.  
  195. /**
  196.  * Base32 decode to a binary string
  197.  *
  198.  * @param    $inString   String to base32 decode
  199.  *
  200.  * @return   $outString  Base32 decoded $inString
  201.  *
  202.  * @access   private
  203.  *
  204.  */
  205.  
  206. function Base32_decode($inString) {
  207.     /* declaration */
  208.     $inputCheck = null;
  209.     $deCompBits = null;
  210.    
  211.     $BASE32_TABLE = array(
  212.                           0x61 => '00000',
  213.                           0x62 => '00001',
  214.                           0x63 => '00010',
  215.                           0x64 => '00011',
  216.                           0x65 => '00100',
  217.                           0x66 => '00101',
  218.                           0x67 => '00110',
  219.                           0x68 => '00111',
  220.                           0x69 => '01000',
  221.                           0x6a => '01001',
  222.                           0x6b => '01010',
  223.                           0x6c => '01011',
  224.                           0x6d => '01100',
  225.                           0x6e => '01101',
  226.                           0x6f => '01110',
  227.                           0x70 => '01111',
  228.                           0x71 => '10000',
  229.                           0x72 => '10001',
  230.                           0x73 => '10010',
  231.                           0x74 => '10011',
  232.                           0x75 => '10100',
  233.                           0x76 => '10101',
  234.                           0x77 => '10110',
  235.                           0x78 => '10111',
  236.                           0x79 => '11000',
  237.                           0x7a => '11001',
  238.                           0x32 => '11010',
  239.                           0x33 => '11011',
  240.                           0x34 => '11100',
  241.                           0x35 => '11101',
  242.                           0x36 => '11110',
  243.                           0x37 => '11111',
  244.                           );
  245.    
  246.     /* Step 1 */
  247.     $inputCheck = strlen($inString) % 8;
  248.     if(($inputCheck == 1)||($inputCheck == 3)||($inputCheck == 6)) {
  249.         trigger_error('input to Base32Decode was a bad mod length: '.$inputCheck);
  250.         return false;
  251.         //return $this->raiseError('input to Base32Decode was a bad mod length: '.$inputCheck, null,
  252.         // PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
  253.     }
  254.    
  255.     /* $deCompBits is a string that represents the bits as 0 and 1.*/
  256.     for ($i = 0; $i < strlen($inString); $i++) {
  257.         $inChar = ord(substr($inString,$i,1));
  258.         if(isset($BASE32_TABLE[$inChar])) {
  259.             $deCompBits .= $BASE32_TABLE[$inChar];
  260.         } else {
  261.             trigger_error('input to Base32Decode had a bad character: '.$inChar);
  262.             return false;
  263.             //return $this->raiseError('input to Base32Decode had a bad character: '.$inChar, null,
  264.             //    PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
  265.         }
  266.     }
  267.    
  268.     /* Step 5 */
  269.     $padding = strlen($deCompBits) % 8;
  270.     $paddingContent = substr($deCompBits, (strlen($deCompBits) - $padding));
  271.     if(substr_count($paddingContent, '1')>0) {
  272.         trigger_error('found non-zero padding in Base32Decode');
  273.         return false;
  274.         //return $this->raiseError('found non-zero padding in Base32Decode', null,
  275.         //    PEAR_ERROR_DIE, null, null, 'Net_RACE_Error', false );
  276.     }
  277.    
  278.     /* Break the decompressed string into octets for returning */
  279.     $deArr = array();
  280.     for($i = 0; $i < (int)(strlen($deCompBits) / 8); $i++) {
  281.         $deArr[$i] = chr(bindec(substr($deCompBits, $i*8, 8)));
  282.     }
  283.    
  284.     $outString = join('',$deArr);
  285.    
  286.     return $outString;
  287. }
  288.  
  289. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement