MDMCK10

PHP Guacamole string encoder and decoder

Jan 11th, 2021 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. <?php
  2. namespace Guacamole;
  3. // why does php not have a built in slice method?
  4. function str_slice() {
  5.     $args = func_get_args();
  6.     $str        = $args[0];
  7.     $str_length = strlen($str);
  8.     $start      = $args[1];
  9.     $end        = $args[2];
  10.     if ($start >= $str_length) {
  11.         return "";
  12.     }
  13.     if ($start < 0) {
  14.         if ($start < - $str_length) {
  15.             $start = 0;
  16.         } else {
  17.             $start = $str_length - abs($start);
  18.         }
  19.     }
  20.     if ($end <= $start) {
  21.         return "";
  22.     }
  23.     if ($end > $str_length) {
  24.         $end = $str_length;
  25.     }
  26.     $length = $end - $start;
  27.     return substr($str, $start, $length);
  28. }
  29.  
  30. function decodeResponse($str) {
  31.   $pos=-1;
  32.   $sections = [];
  33.  
  34.   for(;;) {
  35.       $len = strpos($str,'.',$pos+1);
  36.       if($len==-1) {break;}
  37.       $pos = intval(str_slice($str,$pos+1,$len))+$len+1;
  38.       array_push($sections, str_slice($str,$len+1,$pos));
  39.       if(str_slice($str,$pos,$pos+1)==';'){
  40.         break;
  41.       }
  42.   }
  43.   return $sections;
  44. }
  45.  
  46. function encodeGuac($cypher) {
  47.     $command='';
  48.     for($i=0;$i<count($cypher);$i++){
  49.         $current=$cypher[$i];
  50.         $command.=strval(strlen($current)).".".$current;
  51.         $command.=($i<count($cypher)-1?',':';');
  52.     }
  53.     return $command;
  54. }
  55. ?>
Add Comment
Please, Sign In to add comment