Advertisement
alk0v

PDU Converter

Jun 2nd, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>PDU Converter</title>
  5. <style>
  6. * { font-family:Calibri }
  7. fieldset { margin-bottom: 15px; padding: 10px }
  8. legend { padding: 0px 3px; font-weight: bold; font-variant: small-caps }
  9. select { width: 254px }
  10. input[type=submit] { width: 170px; padding: 10px }
  11. </style>
  12. </head>
  13.  
  14. <body>
  15.  
  16. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
  17.   <fieldset>
  18.     <legend>Enter Text for encode to PDU format</legend>
  19.     <textarea name="inputtext" cols="40" rows="5">
  20.     <?php
  21.         if (isset($_GET['inputtext']) && $_GET['inputtext'])
  22.         {
  23.             echo $_GET['inputtext'];
  24.         }
  25.     ?>
  26.     </textarea>
  27.     <textarea name="Text1" cols="40" rows="5" ... >
  28.     <?php
  29.         if (isset($_GET['inputtext']) && $_GET['inputtext'])
  30.         {
  31.             encodePDU($_GET['inputtext']);
  32.         }
  33.     ?>
  34.     </textarea>
  35.   </fieldset>
  36.   <input type="submit" value="Encode">
  37. </form>
  38. <br>
  39. <br>
  40. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
  41.   <fieldset>
  42.     <legend>Enter PDU for decode to Text format</legend>
  43.     <textarea name="inputpdu" cols="40" rows="5">
  44.     <?php
  45.         if (isset($_GET['inputpdu']) && $_GET['inputpdu'])
  46.         {
  47.             echo $_GET['inputpdu'];
  48.         }
  49.     ?>
  50.     </textarea>
  51.     <textarea name="Text2" cols="40" rows="5" ... >
  52.     <?php
  53.         if (isset($_GET['inputpdu']) && $_GET['inputpdu'])
  54.         {
  55.             decodePDU($_GET['inputpdu']);
  56.         }
  57.     ?>
  58.     </textarea>
  59.   </fieldset>
  60.   <input type="submit" value="Decode">
  61. </form>
  62.  
  63.  
  64.  
  65.  
  66. </body>
  67. </html>
  68.  
  69. <?php
  70. function decodePDU($in) {
  71.   $b = 0; $d = 0;
  72.   $out = "";
  73.   foreach (str_split($in, 2) as $ss) {
  74.     $byte = hexdec($ss);
  75.     $c = (($byte & ((1 << 7-$d)-1)) << $d) | $b;
  76.     $b = $byte >> (7-$d);
  77.     $out .= chr($c);
  78.     $d++;
  79.     if ($d == 7) {
  80.       $out .= chr($b);
  81.       $d = 0; $b = 0;
  82.     }
  83.   }
  84.   echo $out;
  85. }
  86.  
  87. function encodePDU($in) {
  88.   $out = "";
  89.   for ($i = 0; $i < strlen($in); $i++) {
  90.     $t = $i%8+1;
  91.     if ($t == 8)
  92.       continue;
  93.     $c = ord($in[$i])>>($i%8);
  94.     $oc = $c;
  95.     $b = ord($in[$i+1]) & ((1 << $t)-1);
  96.     $c = ($b << (8-$t)) | $c;
  97.     $out .= strtoupper(str_pad(dechex($c), 2, '0', STR_PAD_LEFT));
  98.   }
  99.   echo $out;
  100. }
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement