Guest User

func.php

a guest
Jan 30th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 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 $template_map ;
  65. global $BinColumnDelimiter ;
  66. global $BinLineEnd ;
  67. $decodedfilecontents = "" ;
  68. $handle = fopen($filename, "r");
  69.  
  70.  
  71. if ($handle) {
  72. foreach ( $template as $ColName=>$Endians) $decodedfilecontents .= $ColName.$BinColumnDelimiter;
  73. $decodedfilecontents .= $BinLineEnd;
  74.  
  75. while (!feof($handle)) {
  76. $lastLine = '';
  77. foreach ( $template as $ColName=>$Endians){
  78. $hex = bin2hex(fread($handle,$Endians));
  79. if ( $Endians <= 8) {
  80. $val = hexdec(decodehex($hex));
  81. if ( $val == 4294967295 ) $val = -1;
  82. } else {
  83. $val = str_replace("\t"," ",hex2str($hex));
  84. }
  85. if ( isset($template_map[$ColName][$val]) )
  86. $val = $template_map[$ColName][$val];
  87. $lastLine .= $val.$BinColumnDelimiter;
  88. // echo('Column ['.$ColName.']['.$Endians.'] = ['. str_replace( hex2str('CC'),' ',$val).']<br />');
  89. }
  90. // echo '<hr />';
  91. if ( ! feof($handle))
  92. $decodedfilecontents .= $lastLine.$BinLineEnd;
  93.  
  94. }
  95. fclose($handle);
  96. /* if (!DEMO){
  97. $decodedfile = fopen($output,'w+') ;
  98. fwrite($decodedfile,$decodedfilecontents) ;
  99. fclose($decodedfile) ;
  100. }*/
  101. }
  102. echo '<h1>'.$output.'</h1>';
  103. echo '<hr />';
  104. print_r($decodedfilecontents);
  105. }
  106.  
  107. // ===================================================================================
  108. ## FUNCTION FOR EXTRACTING THE skilllevel.bin FILE
  109. ## I AM TOO LAZY TO EDIT THE TEMPLATES
  110. function extract_skilllevel_bin($filename,$output){
  111. global $template ;
  112. global $BinColumnDelimiter ;
  113. global $BinLineEnd ;
  114. $decodedfilecontents = "" ;
  115. $loops = 0 ;
  116. $temp_template = $template ;
  117. unset($temp_template['ID']) ;
  118. $usedTemplate = $template ;
  119.  
  120. $handle = fopen($filename, "r");
  121. if ($handle) {
  122. foreach ( $template as $ColName=>$Endians) $decodedfilecontents .= $ColName.$BinColumnDelimiter;
  123. $decodedfilecontents .= $BinLineEnd;
  124.  
  125.  
  126. while (!feof($handle)) {
  127. $loops++;
  128. if ( $loops % 7 == 1)
  129. $usedTemplate = $template;
  130. else {
  131. $usedTemplate = $temp_template;
  132. $decodedfilecontents .= ' '.$BinColumnDelimiter;
  133. }
  134. $lastLine = '';
  135.  
  136. foreach ( $usedTemplate as $ColName=>$Endians){
  137. if ( $loops % 7 == 0 && $ColName=='szExplainFileName[128]' ) $Endians = 129;
  138. $hex = bin2hex(fread($handle,$Endians));
  139. if ( $Endians <= 4) {
  140. $val = hexdec(decodehex($hex));
  141. if ( $val == 4294967295 ) $val = -1;
  142. $lastLine.= $val.$BinColumnDelimiter;
  143. } else {
  144. $lastLine.= hex2str($hex).$BinColumnDelimiter;
  145. }
  146. }
  147. if ( ! feof($handle) ) $decodedfilecontents .= $lastLine.$BinLineEnd;
  148. }
  149. fclose($handle);
  150.  
  151. if (!DEMO){
  152. $decodedfile = fopen($output,'w+') ;
  153. fwrite($decodedfile,$decodedfilecontents) ;
  154. fclose($decodedfile) ;
  155. }
  156. }
  157. echo '<h1>'.$output.'</h1>';
  158. echo '<hr />';
  159. print_r($decodedfilecontents);
  160. }
  161.  
  162. // ===================================================================================
  163.  
  164. function encodehex($decimal,$endians){
  165. $hexval = dechex($decimal);
  166. $hexstrlen = strlen($hexval) ;
  167. $hexstrindex = $hexstrlen ;
  168. $temphexstr = "" ;
  169. $result = "" ;
  170.  
  171. while ($hexstrindex > 0 ){
  172. if ( $hexstrindex -2 < 0 ){
  173. $hexstrindex = 2 ;
  174. $len = 1 ;
  175. } else $len = 2 ;
  176. $temphexstr = substr($hexval,$hexstrindex -2, $len );
  177. if ( $len == 1)
  178. $temphexstr = '0'.$temphexstr ;
  179. $hexstrindex -= 2 ;
  180. $result = $result . $temphexstr ;
  181. }
  182. $result = str_pad($result,$endians*2,'0',STR_PAD_RIGHT);
  183. return $result;
  184. }
  185.  
  186. // ===================================================================================
  187.  
  188. function strhex($string, $endians){
  189. $result = "";
  190. $tempdec = "";
  191.  
  192. for ($i = 0;$i<strlen($string);$i++)
  193. $result .= dechex(ord($string[$i]));
  194. $result = str_pad($result,$endians*2,"0",STR_PAD_RIGHT);
  195. return $result;
  196. }
  197.  
  198. // ===================================================================================
  199.  
  200. function compile_bin ( $source, $dest ){
  201. global $template ;
  202. global $template_map ;
  203. global $BinColumnDelimiter ;
  204. global $BinLineEnd ;
  205.  
  206. $binName = explode('.',$dest) ;
  207. $binName = str_replace('recompiled/ex-','',$binName[0]) ;
  208.  
  209.  
  210. $indexed_template = array_values($template) ;
  211. $template_keys = array_keys($template) ;
  212. $contents = file_get_contents($source) ;
  213. $a_contents = explode($BinLineEnd,$contents) ;
  214.  
  215. $handle = fopen($dest,'w+') ;
  216.  
  217. for ( $index = 1; $index < count($a_contents)-1; $index++){
  218. set_time_limit(50);
  219. $line = $a_contents[$index];
  220. $cols = explode($BinColumnDelimiter,$line);
  221.  
  222. ## ci -> column index
  223. ## cv -> column value
  224. for ($ci=0;$ci<count($cols)-1;$ci++){
  225. $cv = $cols[$ci];
  226.  
  227. $map_key = @array_search($cv,$template_map[$template_keys[$ci]]);
  228. if ( isset($template_map[$template_keys[$ci]][$map_key]) )
  229. $cv = $map_key;
  230.  
  231.  
  232. $clen = $indexed_template[$ci];
  233.  
  234. // echo 'Line : '.$index.' Column '.$ci.' Total columns '.(count($cols) -1 ).'<br />';
  235. // echo 'Column Data : '.$cv.'<br />';
  236. // echo 'Column Name : '.$template_keys[$ci].' <hr />';
  237. if ( $clen <= 8) {
  238. fwrite($handle, hex2bin( encodehex($cv,$clen) ) );
  239. } else {
  240. fwrite($handle, hex2bin( strhex($cv,$clen) ) );
  241. }
  242. }
  243. }
  244. fclose($handle);
  245. }
  246.  
  247. // ===================================================================================
  248.  
  249. function compile_skilllevel_bin ( $source, $dest){
  250. global $template ;
  251. global $BinColumnDelimiter ;
  252. global $BinLineEnd ;
  253. $temp_template = $template ;
  254. unset($temp_template['ID']) ;
  255. $used_template = $template ;
  256.  
  257. $indexed_template = array_values($used_template) ;
  258.  
  259. $contents = file_get_contents($source) ;
  260. $a_contents = explode($BinLineEnd,$contents) ;
  261.  
  262. $handle = fopen($dest,'w+') ;
  263.  
  264. for ( $index = 1; $index < count($a_contents)-1;$index++){
  265. set_time_limit(10);
  266. $line = $a_contents[$index];
  267. $cols = explode($BinColumnDelimiter,$line);
  268.  
  269. if ( $index % 7 == 1) {
  270. $used_template = $template;
  271. // echo '<hr />';
  272. } else {
  273. $used_template = $temp_template;
  274. array_shift($cols);
  275. // echo '<br />';
  276. }
  277.  
  278. $tKeys = array_keys($used_template) ;
  279. $indexed_template = array_values($used_template) ;
  280.  
  281. $tKeys = array_keys($used_template);
  282. for ($ci=0;$ci<count($cols);$ci++){
  283. $cv = $cols[$ci] ;
  284. $clen = $indexed_template[$ci] ;
  285.  
  286. // echo 'Line : '.$index.' Column '.$ci.' Total columns '.(count($cols) -1 ).'<br />';
  287. if ( $index % 7 == 0 && $ci == count($cols)-1 ) {
  288. $clen = 129;
  289. }
  290.  
  291. if ( $clen <= 4) {
  292. fwrite($handle, hex2bin( encodehex($cv,$clen) ) );
  293. } else {
  294. fwrite($handle, hex2bin( strhex($cv,$clen) ) );
  295. }
  296. echo $tKeys[$ci].' '.$cv.' '.$clen.'<br />';
  297.  
  298. }
  299.  
  300. }
  301. fclose($handle);
  302. }
  303.  
  304. ?>
Advertisement
Add Comment
Please, Sign In to add comment