Advertisement
Guest User

Untitled

a guest
May 28th, 2012
2,500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. <?php
  2. // AndrewMohawk
  3. // http://www.andrewmohawk.com
  4.  
  5. /* Decode Track 2/3 data from binary */
  6. $binary = "<yourBinaryHere>";
  7.  
  8. // this function by mtroy dot student at gmail dot com taken from http://php.net/manual/en/function.strpos.php
  9. function strpos_r($haystack, $needle)
  10. {
  11.     if(strlen($needle) > strlen($haystack))
  12.         trigger_error(sprintf("%s: length of argument 2 must be <= argument 1", __FUNCTION__), E_USER_WARNING);
  13.  
  14.     $seeks = array();
  15.     while($seek = strrpos($haystack, $needle))
  16.     {
  17.         array_push($seeks, $seek);
  18.         $haystack = substr($haystack, 0, $seek);
  19.     }
  20.     return $seeks;
  21. }
  22.  
  23. function processBinary($binary)
  24. {
  25.     $AsciiOutput = "";
  26.    
  27.     //find start sentinal
  28.     $start_sentinal = strpos($binary,"11010");
  29.     if($start_sentinal === false)
  30.     {
  31.         echo "Could not find start sentinal\n";
  32.         return false;
  33.     }
  34.    
  35.     //find end sentinal
  36.     $end_sentinal = false;
  37.     $end_sentinals = strpos_r($binary,"11111");
  38.     if(count($end_sentinals) == 0)
  39.     {
  40.         echo "Could not find end sentinal\n";
  41.         return false;
  42.     }
  43.    
  44.     //Check end sentinal is on a 5 bit boundry
  45.     foreach($end_sentinals as $es)
  46.     {
  47.         $es = $es;
  48.         if(($es - $start_sentinal) % 5 == 0)
  49.         {
  50.             $end_sentinal = $es;
  51.         }
  52.     }
  53.    
  54.     if($end_sentinal == false)
  55.     {
  56.         echo "End sentinal not on correct boundry\n";
  57.         return false;
  58.     }
  59.    
  60.    
  61.    
  62.     //Lets decode the data:
  63.     $bit_length = 5; // 4 bits for data, 1 bit for odd-parity or LRC checking
  64.    
  65.    
  66.     $data = substr($binary,$start_sentinal,($end_sentinal-$start_sentinal+5));
  67.    
  68.     $currentBits = "";
  69.     $currentNum = 0;
  70.     $finalString = "";
  71.    
  72.     for($i=0;$i<strlen($data);$i++)
  73.     {
  74.         if(strlen($currentBits) < $bit_length)
  75.         {
  76.             $currentBits .= $data[$i];
  77.            
  78.         }
  79.        
  80.         if(strlen($currentBits) == $bit_length)
  81.         {
  82.             $parityBit = $currentBits[4];
  83.             $dataBits = substr($currentBits,0,4);
  84.            
  85.             $asciiChar = 0;
  86.            
  87.            
  88.             for($x=0;$x<4;$x++)
  89.             {
  90.                 $currentNum += $dataBits[$x];
  91.             }
  92.            
  93.            
  94.              
  95.             $dec = bindec($dataBits);
  96.             $dec = str_pad($dec, 2, "0", STR_PAD_LEFT); // just so output is nice
  97.             $asciiChar = chr(bindec(strrev($dataBits))+48); // reverse the binary (since its LSB first) then convert to dec, add 48 and then take it to ASCII
  98.             echo "$currentBits - Data ($dataBits) Parity($parityBit) Decimal ($dec) Ascii($asciiChar)";
  99.             if(($currentNum + $parityBit) % 2 == false)
  100.             {
  101.                 echo " __ Parity: Invalid";
  102.             }
  103.             else
  104.             {
  105.                 echo " __ Parity: Valid";
  106.             }
  107.             $AsciiOutput .= $asciiChar;
  108.             echo "\n";
  109.             $currentBits = "";
  110.             $currentNum = 0;
  111.            
  112.         }
  113.        
  114.        
  115.     }
  116.     echo "\n\nTotal Out (ascii): $AsciiOutput\n";
  117. }
  118. echo "Trying One way:\n\n";
  119. if (processBinary($binary) == false)
  120. {
  121.     //reverse.
  122.     echo "\n\n";
  123.     echo "Trying The Reverse:\n\n";
  124.     processBinary(strrev($binary));
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement