Advertisement
Guest User

func3.php

a guest
Jan 30th, 2014
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. <?php
  2.  
  3. global $BinColumnDelimiter ;
  4. global $BinLineEnd ;
  5.  
  6. $BinColumnDelimiter = "\t" ;
  7. $BinLineEnd = "\r\n" ;
  8.  
  9. function hex2bin($hexstr){
  10. $n = strlen($hexstr);
  11. $sbin="";
  12. $i=0;
  13. while($i<$n){
  14. $a =substr($hexstr,$i,2);
  15. $c = pack("H*",$a);
  16. if ($i==0){$sbin=$c;}
  17. else {$sbin.=$c;}
  18. $i+=2;
  19. }
  20. return $sbin;
  21. }
  22.  
  23. // ===================================================================================
  24.  
  25. function decodehex($hexstr){
  26. $strIndex = strlen($hexstr) ;
  27. $step = 2 ;
  28. $tempHex = "" ;
  29. $result = "" ;
  30. $foundNonNull = false ;
  31.  
  32. while ( $strIndex > 0 ){
  33. $tempHex = substr($hexstr,$strIndex-$step,$step);
  34. $strIndex = $strIndex - $step;
  35. if ( $tempHex != "00"){
  36. $result .= $tempHex;
  37. $foundNonNull = true;
  38. } else {
  39. if ($foundNonNull){
  40. $result .= $tempHex;
  41. }
  42. }
  43. }
  44. return $result;
  45. }
  46.  
  47. // ===================================================================================
  48.  
  49. function hex2str ( $hexstr ) {
  50. $strlen = strlen($hexstr)/2;
  51. $endian = "";
  52. $result = "";
  53. for ( $i = 0; $i<$strlen;$i++){
  54. $endian = substr($hexstr,$i*2,2);
  55. $result .= chr( hexdec($endian) );
  56. }
  57. return $result;
  58. }
  59.  
  60. // ===================================================================================
  61.  
  62. function extract_bin($filename,$output){
  63. global $template ;
  64. global $BinColumnDelimiter ;
  65. global $BinLineEnd ;
  66. $decodedfile = fopen($output,'w+') ;
  67. $decodedfilecontents = "" ;
  68.  
  69. $handle = fopen($filename, "r");
  70. if ($handle) {
  71. foreach ( $template as $ColName=>$Endians) $decodedfilecontents .= $ColName.$BinColumnDelimiter;
  72. $decodedfilecontents .= $BinLineEnd;
  73.  
  74. while (!feof($handle)) {
  75. $lastLine = '';
  76. foreach ( $template as $ColName=>$Endians){
  77. $hex = bin2hex(fread($handle,$Endians));
  78. if ( $Endians <= 8) {
  79. $val = hexdec(decodehex($hex));
  80. if ( $val == 4294967295 ) $val = -1;
  81. $lastLine .= $val.$BinColumnDelimiter;
  82. } else {
  83. $lastLine .= str_replace("\t"," ",hex2str($hex)).$BinColumnDelimiter;
  84. }
  85. }
  86. if ( ! feof($handle))
  87. $decodedfilecontents .= $lastLine.$BinLineEnd;
  88.  
  89. }
  90. fclose($handle);
  91.  
  92. fwrite($decodedfile,$decodedfilecontents);
  93. fclose($decodedfile);
  94. }
  95. echo '<h1>'.$output.'</h1>';
  96. echo '<hr />';
  97. print_r($decodedfilecontents);
  98. }
  99.  
  100. // ===================================================================================
  101. ## FUNCTION FOR EXTRACTING THE skilllevel.bin FILE
  102. ## I AM TOO LAZY TO EDIT THE TEMPLATES
  103. function extract_skilllevel_bin($filename,$output){
  104. global $template ;
  105. global $BinColumnDelimiter ;
  106. global $BinLineEnd ;
  107. $decodedfile = fopen($output,'w+') ;
  108. $decodedfilecontents = "" ;
  109. $loops = 0 ;
  110. $temp_template = $template ;
  111. unset($temp_template['ID']) ;
  112. $usedTemplate = $template ;
  113.  
  114. $handle = fopen($filename, "r");
  115. if ($handle) {
  116. foreach ( $template as $ColName=>$Endians) $decodedfilecontents .= $ColName.$BinColumnDelimiter;
  117. $decodedfilecontents .= $BinLineEnd;
  118.  
  119.  
  120. while (!feof($handle)) {
  121. $loops++;
  122. if ( $loops % 7 == 1)
  123. $usedTemplate = $template;
  124. else {
  125. $usedTemplate = $temp_template;
  126. $decodedfilecontents .= ' '.$BinColumnDelimiter;
  127. }
  128. $lastLine = '';
  129.  
  130. foreach ( $usedTemplate as $ColName=>$Endians){
  131. if ( $loops % 7 == 0 && $ColName=='szExplainFileName[128]' ) $Endians = 129;
  132. $hex = bin2hex(fread($handle,$Endians));
  133. if ( $Endians <= 4) {
  134. $val = hexdec(decodehex($hex));
  135. if ( $val == 4294967295 ) $val = -1;
  136. $lastLine.= $val.$BinColumnDelimiter;
  137. } else {
  138. $lastLine.= hex2str($hex).$BinColumnDelimiter;
  139. }
  140. }
  141. if ( ! feof($handle) ) $decodedfilecontents .= $lastLine.$BinLineEnd;
  142. }
  143. fclose($handle);
  144.  
  145. fwrite($decodedfile,$decodedfilecontents);
  146. fclose($decodedfile);
  147. }
  148. echo '<h1>'.$output.'</h1>';
  149. echo '<hr />';
  150. print_r($decodedfilecontents);
  151. }
  152.  
  153. // ===================================================================================
  154.  
  155. function encodehex($decimal,$endians){
  156. $hexval = dechex($decimal);
  157. $hexstrlen = strlen($hexval) ;
  158. $hexstrindex = $hexstrlen ;
  159. $temphexstr = "" ;
  160. $result = "" ;
  161.  
  162. while ($hexstrindex > 0 ){
  163. if ( $hexstrindex -2 < 0 ){
  164. $hexstrindex = 2 ;
  165. $len = 1 ;
  166. } else $len = 2 ;
  167. $temphexstr = substr($hexval,$hexstrindex -2, $len );
  168. if ( $len == 1)
  169. $temphexstr = '0'.$temphexstr ;
  170. $hexstrindex -= 2 ;
  171. $result = $result . $temphexstr ;
  172. }
  173. $result = str_pad($result,$endians*2,'0',STR_PAD_RIGHT);
  174. return $result;
  175. }
  176.  
  177. // ===================================================================================
  178.  
  179. function strhex($string, $endians){
  180. $result = "";
  181. $tempdec = "";
  182.  
  183. for ($i = 0;$i<strlen($string);$i++)
  184. $result .= dechex(ord($string[$i]));
  185. $result = str_pad($result,$endians*2,"0",STR_PAD_RIGHT);
  186. return $result;
  187. }
  188.  
  189. // ===================================================================================
  190.  
  191. function compile_bin ( $source, $dest){
  192. global $template;
  193. global $BinColumnDelimiter;
  194. global $BinLineEnd;
  195.  
  196. $indexed_template = array_values($template) ;
  197.  
  198. $contents = file_get_contents($source) ;
  199. $a_contents = explode($BinLineEnd,$contents) ;
  200.  
  201. $handle = fopen($dest,'w+') ;
  202.  
  203. for ( $index = 1; $index < count($a_contents)-1;$index++){
  204. $line = $a_contents[$index];
  205. $cols = explode($BinColumnDelimiter,$line);
  206. for ($ci=0;$ci<count($cols);$ci++){
  207. $cv = $cols[$ci];
  208. $clen = $indexed_template[$ci];
  209. echo 'Line : '.$index.' Column '.$ci.' Total columns '.(count($cols) -1 ).'<br />';
  210. echo 'Column Data : '.$cv.'<hr />';
  211. if ( $clen <= 8) {
  212. fwrite($handle, hex2bin( encodehex($cv,$clen) ) );
  213. } else {
  214. fwrite($handle, hex2bin( strhex($cv,$clen) ) );
  215. }
  216. }
  217. }
  218. fclose($handle);
  219. }
  220.  
  221. // ===================================================================================
  222.  
  223. function compile_skilllevel_bin ( $source, $dest){
  224. global $template ;
  225. global $BinColumnDelimiter ;
  226. global $BinLineEnd ;
  227. $temp_template = $template ;
  228. unset($temp_template['ID']) ;
  229. $used_template = $template ;
  230.  
  231. $indexed_template = array_values($used_template) ;
  232.  
  233. $contents = file_get_contents($source) ;
  234. $a_contents = explode($BinLineEnd,$contents) ;
  235.  
  236. $handle = fopen($dest,'w+') ;
  237.  
  238. for ( $index = 1; $index < count($a_contents)-1;$index++){
  239. $line = $a_contents[$index];
  240. $cols = explode($BinColumnDelimiter,$line);
  241.  
  242. if ( $index % 7 == 1) {
  243. $used_template = $template;
  244. echo '<hr />';
  245. } else {
  246. $used_template = $temp_template;
  247. array_shift($cols);
  248. echo '<br />';
  249. }
  250.  
  251. $tKeys = array_keys($used_template) ;
  252. $indexed_template = array_values($used_template) ;
  253.  
  254. $tKeys = array_keys($used_template);
  255. for ($ci=0;$ci<count($cols);$ci++){
  256. $cv = $cols[$ci] ;
  257. $clen = $indexed_template[$ci] ;
  258.  
  259. echo 'Line : '.$index.' Column '.$ci.' Total columns '.(count($cols) -1 ).'<br />';
  260. if ( $index % 7 == 0 && $ci == count($cols)-1 ) {
  261. $clen = 129;
  262. }
  263.  
  264. if ( $clen <= 4) {
  265. fwrite($handle, hex2bin( encodehex($cv,$clen) ) );
  266. } else {
  267. fwrite($handle, hex2bin( strhex($cv,$clen) ) );
  268. }
  269. echo $tKeys[$ci].' '.$cv.' '.$clen.'<br />';
  270.  
  271. }
  272.  
  273. }
  274. fclose($handle);
  275. }
  276.  
  277. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement