3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Example: Decode an Input Stream with PHP filters * * decode-input-stream-example.php * * @link https://gist.github.com/hakre/d34239bb237c50e728fd * @link http://stackoverflow.com/q/25051578/367456 */ # given a file which contains hex-encoded binary sequences that are partially disruped by US-ASCII control characters $transport = '<?xml version=\x221.0\x22 encoding=\x22UTF-8\x22?> \x0A<issues>\x0A\x09<issue id=\x225863\x22 found=\x221\x22>\xD0\x9F\xD0\xBE \xD0\xBD\xD0\xBE\xD0\xBC\xD0\xB5\xD1\x80\xD1\x83 \xD1\x81\xD1\x87\xD0\xB 5\xD1\x82\xD0\xB0 19479 \xD0\xBD\xD0\xB0\xD0\xB9\xD0\xB4\xD0\xB5\xD0\xBD\xD0\xBE:\x0A\xD0\x97\xD0\xB0\xD0\xBA\xD0\xB0\xD0\xB7 \xD0\xBF\xD0\xBE\xD0\xBA\xD1\x83\xD0\xBF\xD0\xB0\xD1\x82\xD0\xB5\xD0\xBB\xD1\x8F 0000015597 \xD0\xBE...</issue></issues>'; $file = 'data://text/plain;base64,' . base64_encode($transport); # registering two filters, one to remove control characters, the other to decode hex-sequences stream_filter_register('filter.controlchars', 'ControlCharsFilter'); stream_filter_register('decode.hexsequences', 'HexDecodeFilter'); # allows to chain those filters and to obtain the decoded (raw) data on-the-fly $filters = 'filter.controlchars/decode.hexsequences'; $xml = simplexml_load_file("php://filter/read=$filters/resource=" . $file); echo "isseu id: ", $xml->issue['id'], "\n"; $xml->asXML('php://stdout'); /** * Class ReadFilter */ abstract class ReadFilter extends php_user_filter { function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { $bucket->data = $this->apply($bucket->data); $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } abstract function apply($string); } /** * Class ControlCharsFilter */ class ControlCharsFilter extends ReadFilter { function apply($string) { return preg_replace('~[\x00-\x1f]+~', '', $string); } } /** * Class HexDecodeFilter */ class HexDecodeFilter extends ReadFilter { function apply($string) { return preg_replace_callback( '/\\\\x([A-F0-9]{2})/i', 'self::decodeHexMatches' , $string ); } private static function decodeHexMatches($matches) { return hex2bin($matches[1]); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IO6Ll
function name:  (null)
number of ops:  32
compiled vars:  !0 = $transport, !1 = $file, !2 = $filters, !3 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   ASSIGN                                                   !0, '%3C%3Fxml+version%3D%5Cx221.0%5Cx22+encoding%3D%5Cx22UTF-8%5Cx22%3F%3E%0A%5Cx0A%3Cissues%3E%5Cx0A%5Cx09%3Cissue+id%3D%5Cx225863%5Cx22+found%3D%5Cx221%5Cx22%3E%5CxD0%5Cx9F%5CxD0%5CxBE+%5CxD0%5CxBD%5CxD0%5CxBE%5CxD0%5CxBC%5CxD0%5CxB5%5CxD1%5Cx80%5CxD1%5Cx83+%5CxD1%5Cx81%5CxD1%5Cx87%5CxD0%5CxB%0A5%5CxD1%5Cx82%5CxD0%5CxB0+19479+%5CxD0%5CxBD%5CxD0%5CxB0%5CxD0%5CxB9%5CxD0%5CxB4%5CxD0%5CxB5%5CxD0%5CxBD%5CxD0%5CxBE%3A%5Cx0A%5CxD0%5Cx97%5CxD0%5CxB0%5CxD0%5CxBA%5CxD0%5CxB0%5CxD0%5CxB7+%5CxD0%5CxBF%5CxD0%5CxBE%5CxD0%5CxBA%5CxD1%5Cx83%5CxD0%5CxBF%5CxD0%5CxB0%5CxD1%5Cx82%5CxD0%5CxB5%5CxD0%5CxBB%5CxD1%5Cx8F%0A0000015597+%5CxD0%5CxBE...%3C%2Fissue%3E%3C%2Fissues%3E'
   16     1        INIT_FCALL                                               'base64_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $5      
          4        CONCAT                                           ~6      'data%3A%2F%2Ftext%2Fplain%3Bbase64%2C', $5
          5        ASSIGN                                                   !1, ~6
   19     6        INIT_FCALL                                               'stream_filter_register'
          7        SEND_VAL                                                 'filter.controlchars'
          8        SEND_VAL                                                 'ControlCharsFilter'
          9        DO_ICALL                                                 
   20    10        INIT_FCALL                                               'stream_filter_register'
         11        SEND_VAL                                                 'decode.hexsequences'
         12        SEND_VAL                                                 'HexDecodeFilter'
         13        DO_ICALL                                                 
   23    14        ASSIGN                                                   !2, 'filter.controlchars%2Fdecode.hexsequences'
   24    15        INIT_FCALL                                               'simplexml_load_file'
         16        ROPE_INIT                                     3  ~12     'php%3A%2F%2Ffilter%2Fread%3D'
         17        ROPE_ADD                                      1  ~12     ~12, !2
         18        ROPE_END                                      2  ~11     ~12, '%2Fresource%3D'
         19        CONCAT                                           ~14     ~11, !1
         20        SEND_VAL                                                 ~14
         21        DO_ICALL                                         $15     
         22        ASSIGN                                                   !3, $15
   26    23        ECHO                                                     'isseu+id%3A+'
         24        FETCH_OBJ_R                                      ~17     !3, 'issue'
         25        FETCH_DIM_R                                      ~18     ~17, 'id'
         26        ECHO                                                     ~18
         27        ECHO                                                     '%0A'
   27    28        INIT_METHOD_CALL                                         !3, 'asXML'
         29        SEND_VAL_EX                                              'php%3A%2F%2Fstdout'
         30        DO_FCALL                                      0          
   68    31      > RETURN                                                   1

Class ReadFilter:
Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
Branch analysis from position: 5
filename:       /in/IO6Ll
function name:  filter
number of ops:  25
compiled vars:  !0 = $in, !1 = $out, !2 = $consumed, !3 = $closing, !4 = $bucket
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   34     4      > JMP                                                      ->18
   35     5    >   INIT_METHOD_CALL                                         'apply'
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $6      !4, 'data'
          8        SEND_FUNC_ARG                                            $6
          9        DO_FCALL                                      0  $7      
         10        ASSIGN_OBJ                                               !4, 'data'
         11        OP_DATA                                                  $7
   36    12        FETCH_OBJ_R                                      ~8      !4, 'datalen'
         13        ASSIGN_OP                                     1          !2, ~8
   37    14        INIT_FCALL                                               'stream_bucket_append'
         15        SEND_VAR                                                 !1
         16        SEND_VAR                                                 !4
         17        DO_ICALL                                                 
   34    18    >   INIT_FCALL                                               'stream_bucket_make_writeable'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $11     
         21        ASSIGN                                           ~12     !4, $11
         22      > JMPNZ                                                    ~12, ->5
   39    23    > > RETURN                                                   2
   40    24*     > RETURN                                                   null

End of function filter

Function apply:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IO6Ll
function name:  apply
number of ops:  2
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function apply

End of class ReadFilter.

Class ControlCharsFilter:
Function apply:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IO6Ll
function name:  apply
number of ops:  8
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   50     1        INIT_FCALL                                               'preg_replace'
          2        SEND_VAL                                                 '%7E%5B%5Cx00-%5Cx1f%5D%2B%7E'
          3        SEND_VAL                                                 ''
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $1      
          6      > RETURN                                                   $1
   51     7*     > RETURN                                                   null

End of function apply

Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
Branch analysis from position: 5
filename:       /in/IO6Ll
function name:  filter
number of ops:  25
compiled vars:  !0 = $in, !1 = $out, !2 = $consumed, !3 = $closing, !4 = $bucket
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   34     4      > JMP                                                      ->18
   35     5    >   INIT_METHOD_CALL                                         'apply'
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $6      !4, 'data'
          8        SEND_FUNC_ARG                                            $6
          9        DO_FCALL                                      0  $7      
         10        ASSIGN_OBJ                                               !4, 'data'
         11        OP_DATA                                                  $7
   36    12        FETCH_OBJ_R                                      ~8      !4, 'datalen'
         13        ASSIGN_OP                                     1          !2, ~8
   37    14        INIT_FCALL                                               'stream_bucket_append'
         15        SEND_VAR                                                 !1
         16        SEND_VAR                                                 !4
         17        DO_ICALL                                                 
   34    18    >   INIT_FCALL                                               'stream_bucket_make_writeable'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $11     
         21        ASSIGN                                           ~12     !4, $11
         22      > JMPNZ                                                    ~12, ->5
   39    23    > > RETURN                                                   2
   40    24*     > RETURN                                                   null

End of function filter

End of class ControlCharsFilter.

Class HexDecodeFilter:
Function apply:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IO6Ll
function name:  apply
number of ops:  8
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   59     1        INIT_FCALL                                               'preg_replace_callback'
   60     2        SEND_VAL                                                 '%2F%5C%5Cx%28%5BA-F0-9%5D%7B2%7D%29%2Fi'
          3        SEND_VAL                                                 'self%3A%3AdecodeHexMatches'
   61     4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $1      
          6      > RETURN                                                   $1
   63     7*     > RETURN                                                   null

End of function apply

Function decodehexmatches:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IO6Ll
function name:  decodeHexMatches
number of ops:  7
compiled vars:  !0 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   66     1        INIT_FCALL                                               'hex2bin'
          2        FETCH_DIM_R                                      ~1      !0, 1
          3        SEND_VAL                                                 ~1
          4        DO_ICALL                                         $2      
          5      > RETURN                                                   $2
   67     6*     > RETURN                                                   null

End of function decodehexmatches

Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
Branch analysis from position: 5
filename:       /in/IO6Ll
function name:  filter
number of ops:  25
compiled vars:  !0 = $in, !1 = $out, !2 = $consumed, !3 = $closing, !4 = $bucket
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   34     4      > JMP                                                      ->18
   35     5    >   INIT_METHOD_CALL                                         'apply'
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $6      !4, 'data'
          8        SEND_FUNC_ARG                                            $6
          9        DO_FCALL                                      0  $7      
         10        ASSIGN_OBJ                                               !4, 'data'
         11        OP_DATA                                                  $7
   36    12        FETCH_OBJ_R                                      ~8      !4, 'datalen'
         13        ASSIGN_OP                                     1          !2, ~8
   37    14        INIT_FCALL                                               'stream_bucket_append'
         15        SEND_VAR                                                 !1
         16        SEND_VAR                                                 !4
         17        DO_ICALL                                                 
   34    18    >   INIT_FCALL                                               'stream_bucket_make_writeable'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $11     
         21        ASSIGN                                           ~12     !4, $11
         22      > JMPNZ                                                    ~12, ->5
   39    23    > > RETURN                                                   2
   40    24*     > RETURN                                                   null

End of function filter

End of class HexDecodeFilter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.45 ms | 1412 KiB | 29 Q