Guest User

Untitled

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