3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generate_xor_key($length) { $result = array_fill(0, $length, 0); for ($i = 0, $bit = 1; $i < $length; $i++) { for ($j = 0; $j < 3; $j++, $bit++) { $result[$i] |= ($bit % 2) << $j; } } return implode('', array_map('chr', $result)); } function encode_id($id, $encodedLength = 7, $rawBits = 16, $key = null) { $maxRawBits = $encodedLength * 3; if ($rawBits > $maxRawBits) { trigger_error('encode_id(): $rawBits must be no more than 3 times greater than $encodedLength'); return false; } if ($key === null) { $key = generate_xor_key($encodedLength); } $result = array_fill(0, $encodedLength, 0x30); for ($position = 0; $position < $rawBits; $position++) { $bit = (($id >> $position) & 0x01) << floor($position / $encodedLength); $index = $position % $encodedLength; $result[$index] |= $bit; } do { $index = $position % $encodedLength; $bit = ($position % 2) << floor($position / $encodedLength); $result[$index] |= $bit; } while (++$position < $maxRawBits); return implode('', array_map('chr', $result)) ^ $key; } function decode_id($id, $encodedLength = 7, $rawBits = 16, $key = null) { if ($key === null) { $key = generate_xor_key($encodedLength); } $bytes = array_map( 'ord', str_split( str_pad($id, $encodedLength, '0', STR_PAD_LEFT) ^ $key, 1 ) ); $result = 0; for ($position = 0; $position < $rawBits; $position++) { $index = $position % $encodedLength; $bit = (($bytes[$index] >> floor($position / $encodedLength)) & 0x01) << $position; $result |= $bit; } return $result; } //$key = "\x07\x04\x02\x03\x07\x04\x02"; for ($i = 1; $i <= 1048576; $i++) { if (preg_match('/\D/', encode_id($i, 7, 20, $key))) { echo "Fail at $i\n"; } }
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 = 20, Position 2 = 2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 2
Branch analysis from position: 20
Branch analysis from position: 2
Branch analysis from position: 17
filename:       /in/XFumR
function name:  (null)
number of ops:  21
compiled vars:  !0 = $i, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   ASSIGN                                                   !0, 1
          1      > JMP                                                      ->18
   73     2    >   INIT_FCALL                                               'preg_match'
          3        SEND_VAL                                                 '%2F%5CD%2F'
          4        INIT_FCALL                                               'encode_id'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 7
          7        SEND_VAL                                                 20
          8        SEND_VAR                                                 !1
          9        DO_FCALL                                      0  $3      
         10        SEND_VAR                                                 $3
         11        DO_ICALL                                         $4      
         12      > JMPZ                                                     $4, ->17
   74    13    >   ROPE_INIT                                     3  ~6      'Fail+at+'
         14        ROPE_ADD                                      1  ~6      ~6, !0
         15        ROPE_END                                      2  ~5      ~6, '%0A'
         16        ECHO                                                     ~5
   72    17    >   PRE_INC                                                  !0
         18    >   IS_SMALLER_OR_EQUAL                                      !0, 1048576
         19      > JMPNZ                                                    ~9, ->2
   76    20    > > RETURN                                                   1

Function generate_xor_key:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 10
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 12
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 10
Branch analysis from position: 23
Branch analysis from position: 10
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 12
Branch analysis from position: 20
Branch analysis from position: 12
filename:       /in/XFumR
function name:  generate_xor_key
number of ops:  33
compiled vars:  !0 = $length, !1 = $result, !2 = $i, !3 = $bit, !4 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_fill'
          2        SEND_VAL                                                 0
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !1, $5
    7     7        ASSIGN                                                   !2, 0
          8        ASSIGN                                                   !3, 1
          9      > JMP                                                      ->21
    8    10    >   ASSIGN                                                   !4, 0
         11      > JMP                                                      ->18
    9    12    >   MOD                                              ~11     !3, 2
         13        SL                                               ~12     ~11, !4
         14        ASSIGN_DIM_OP                |=               9          !1, !2
         15        OP_DATA                                                  ~12
    8    16        PRE_INC                                                  !4
         17        PRE_INC                                                  !3
         18    >   IS_SMALLER                                               !4, 3
         19      > JMPNZ                                                    ~15, ->12
    7    20    >   PRE_INC                                                  !2
         21    >   IS_SMALLER                                               !2, !0
         22      > JMPNZ                                                    ~17, ->10
   13    23    >   INIT_FCALL                                               'implode'
         24        SEND_VAL                                                 ''
         25        INIT_FCALL                                               'array_map'
         26        SEND_VAL                                                 'chr'
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                         $18     
         29        SEND_VAR                                                 $18
         30        DO_ICALL                                         $19     
         31      > RETURN                                                   $19
   14    32*     > RETURN                                                   null

End of function generate_xor_key

Function encode_id:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 26
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 41
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 26
Branch analysis from position: 41
Branch analysis from position: 26
Branch analysis from position: 18
filename:       /in/XFumR
function name:  encode_id
number of ops:  66
compiled vars:  !0 = $id, !1 = $encodedLength, !2 = $rawBits, !3 = $key, !4 = $maxRawBits, !5 = $result, !6 = $position, !7 = $bit, !8 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      7
          2        RECV_INIT                                        !2      16
          3        RECV_INIT                                        !3      null
   18     4        MUL                                              ~9      !1, 3
          5        ASSIGN                                                   !4, ~9
   19     6        IS_SMALLER                                               !4, !2
          7      > JMPZ                                                     ~11, ->12
   20     8    >   INIT_FCALL                                               'trigger_error'
          9        SEND_VAL                                                 'encode_id%28%29%3A+%24rawBits+must+be+no+more+than+3+times+greater+than+%24encodedLength'
         10        DO_ICALL                                                 
   21    11      > RETURN                                                   <false>
   24    12    >   TYPE_CHECK                                    2          !3
         13      > JMPZ                                                     ~13, ->18
   25    14    >   INIT_FCALL                                               'generate_xor_key'
         15        SEND_VAR                                                 !1
         16        DO_FCALL                                      0  $14     
         17        ASSIGN                                                   !3, $14
   28    18    >   INIT_FCALL                                               'array_fill'
         19        SEND_VAL                                                 0
         20        SEND_VAR                                                 !1
         21        SEND_VAL                                                 48
         22        DO_ICALL                                         $16     
         23        ASSIGN                                                   !5, $16
   30    24        ASSIGN                                                   !6, 0
         25      > JMP                                                      ->39
   31    26    >   SR                                               ~19     !0, !6
         27        BW_AND                                           ~20     ~19, 1
         28        INIT_FCALL                                               'floor'
         29        DIV                                              ~21     !6, !1
         30        SEND_VAL                                                 ~21
         31        DO_ICALL                                         $22     
         32        SL                                               ~23     ~20, $22
         33        ASSIGN                                                   !7, ~23
   32    34        MOD                                              ~25     !6, !1
         35        ASSIGN                                                   !8, ~25
   33    36        ASSIGN_DIM_OP                |=               9          !5, !8
         37        OP_DATA                                                  !7
   30    38        PRE_INC                                                  !6
         39    >   IS_SMALLER                                               !6, !2
         40      > JMPNZ                                                    ~29, ->26
   37    41    >   MOD                                              ~30     !6, !1
         42        ASSIGN                                                   !8, ~30
   38    43        MOD                                              ~32     !6, 2
         44        INIT_FCALL                                               'floor'
         45        DIV                                              ~33     !6, !1
         46        SEND_VAL                                                 ~33
         47        DO_ICALL                                         $34     
         48        SL                                               ~35     ~32, $34
         49        ASSIGN                                                   !7, ~35
   39    50        ASSIGN_DIM_OP                |=               9          !5, !8
         51        OP_DATA                                                  !7
   40    52        PRE_INC                                          ~38     !6
         53        IS_SMALLER                                               ~38, !4
         54      > JMPNZ                                                    ~39, ->41
   42    55    >   INIT_FCALL                                               'implode'
         56        SEND_VAL                                                 ''
         57        INIT_FCALL                                               'array_map'
         58        SEND_VAL                                                 'chr'
         59        SEND_VAR                                                 !5
         60        DO_ICALL                                         $40     
         61        SEND_VAR                                                 $40
         62        DO_ICALL                                         $41     
         63        BW_XOR                                           ~42     !3, $41
         64      > RETURN                                                   ~42
   43    65*     > RETURN                                                   null

End of function encode_id

Function decode_id:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 29
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 29
Branch analysis from position: 44
Branch analysis from position: 29
Branch analysis from position: 10
filename:       /in/XFumR
function name:  decode_id
number of ops:  46
compiled vars:  !0 = $id, !1 = $encodedLength, !2 = $rawBits, !3 = $key, !4 = $bytes, !5 = $result, !6 = $position, !7 = $index, !8 = $bit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      7
          2        RECV_INIT                                        !2      16
          3        RECV_INIT                                        !3      null
   47     4        TYPE_CHECK                                    2          !3
          5      > JMPZ                                                     ~9, ->10
   48     6    >   INIT_FCALL                                               'generate_xor_key'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $10     
          9        ASSIGN                                                   !3, $10
   51    10    >   INIT_FCALL                                               'array_map'
   52    11        SEND_VAL                                                 'ord'
   53    12        INIT_FCALL                                               'str_split'
   54    13        INIT_FCALL                                               'str_pad'
         14        SEND_VAR                                                 !0
         15        SEND_VAR                                                 !1
         16        SEND_VAL                                                 '0'
         17        SEND_VAL                                                 0
         18        DO_ICALL                                         $12     
         19        BW_XOR                                           ~13     !3, $12
         20        SEND_VAL                                                 ~13
   55    21        SEND_VAL                                                 1
         22        DO_ICALL                                         $14     
         23        SEND_VAR                                                 $14
         24        DO_ICALL                                         $15     
   51    25        ASSIGN                                                   !4, $15
   59    26        ASSIGN                                                   !5, 0
   61    27        ASSIGN                                                   !6, 0
         28      > JMP                                                      ->42
   62    29    >   MOD                                              ~19     !6, !1
         30        ASSIGN                                                   !7, ~19
   63    31        FETCH_DIM_R                                      ~21     !4, !7
         32        INIT_FCALL                                               'floor'
         33        DIV                                              ~22     !6, !1
         34        SEND_VAL                                                 ~22
         35        DO_ICALL                                         $23     
         36        SR                                               ~24     ~21, $23
         37        BW_AND                                           ~25     ~24, 1
         38        SL                                               ~26     ~25, !6
         39        ASSIGN                                                   !8, ~26
   64    40        ASSIGN_OP                                     9          !5, !8
   61    41        PRE_INC                                                  !6
         42    >   IS_SMALLER                                               !6, !2
         43      > JMPNZ                                                    ~30, ->29
   67    44    > > RETURN                                                   !5
   68    45*     > RETURN                                                   null

End of function decode_id

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.65 ms | 1419 KiB | 32 Q