3v4l.org

run code in 300+ PHP versions simultaneously
<?php function ipv6_addr_in_subnet($addr, $subnet, $mask = null) { $addrBin = @inet_pton($addr); if (!$addrBin) { throw new \InvalidArgumentException('Invalid IPv6 address: ' . $addr); } $cidr = 0; if (isset($mask)) { if (is_int($mask)) { $cidr = (int)$mask; if ($cidr < 0 || $cidr > 128) { throw new \InvalidArgumentException('Invalid IPv6 CIDR mask: ' . $mask); } } else { $maskBin = @inet_pton($mask); if (!$maskBin) { throw new \InvalidArgumentException('Invalid IPv6 mask: ' . $mask); } } $subnetAddr = $subnet; } else { $parts = explode('/', $subnet); $numParts = count($parts); if ($numParts === 1) { $maskBin = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; } else if ($numParts === 2) { $cidr = (int)$parts[1]; } else { throw new \InvalidArgumentException('Invalid IPv6 subnet'); } $subnetAddr = $parts[0]; } if (!isset($maskBin)) { $blocks = (int)($cidr / 16); $maskBin = pack( 'n*', ...array_fill(0, $blocks, 0xffff), ...array_pad([((2 ** (16 - ($cidr % 16))) - 1) ^ 0xffff], 8 - $blocks, 0) ); } $subnetAddrBin = @inet_pton($subnetAddr); if (!$subnetAddrBin) { throw new \InvalidArgumentException('Invalid IPv6 address: ' . $subnetAddr); } return ($addrBin & $maskBin) === ($subnetAddrBin & $maskBin); } var_dump(ipv6_addr_in_subnet('2001::1', '2001::/16')); var_dump(ipv6_addr_in_subnet('2001::1', '2000::/15')); var_dump(ipv6_addr_in_subnet('2001::1', '2001::/15')); var_dump(ipv6_addr_in_subnet('2001::1', '2001::/120'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/792TV
function name:  (null)
number of ops:  29
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'ipv6_addr_in_subnet'
          2        SEND_VAL                                                 '2001%3A%3A1'
          3        SEND_VAL                                                 '2001%3A%3A%2F16'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   59     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL                                               'ipv6_addr_in_subnet'
          9        SEND_VAL                                                 '2001%3A%3A1'
         10        SEND_VAL                                                 '2000%3A%3A%2F15'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   60    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'ipv6_addr_in_subnet'
         16        SEND_VAL                                                 '2001%3A%3A1'
         17        SEND_VAL                                                 '2001%3A%3A%2F15'
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   61    21        INIT_FCALL                                               'var_dump'
         22        INIT_FCALL                                               'ipv6_addr_in_subnet'
         23        SEND_VAL                                                 '2001%3A%3A1'
         24        SEND_VAL                                                 '2001%3A%3A%2F120'
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Function ipv6_addr_in_subnet:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 49
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 34
Branch analysis from position: 21
2 jumps found. (Code = 47) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 33
Branch analysis from position: 28
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 102
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 110, Position 2 = 115
Branch analysis from position: 110
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 115
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 102
Branch analysis from position: 27
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 47
Branch analysis from position: 42
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 47
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 60
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 102
Branch analysis from position: 75
Branch analysis from position: 102
Branch analysis from position: 60
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 66
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 66
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/792TV
function name:  ipv6_addr_in_subnet
number of ops:  120
compiled vars:  !0 = $addr, !1 = $subnet, !2 = $mask, !3 = $addrBin, !4 = $cidr, !5 = $maskBin, !6 = $subnetAddr, !7 = $parts, !8 = $numParts, !9 = $blocks, !10 = $subnetAddrBin
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
    5     3        BEGIN_SILENCE                                    ~11     
          4        INIT_FCALL                                               'inet_pton'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $12     
          7        END_SILENCE                                              ~11
          8        ASSIGN                                                   !3, $12
    6     9        BOOL_NOT                                         ~14     !3
         10      > JMPZ                                                     ~14, ->16
    7    11    >   NEW                                              $15     'InvalidArgumentException'
         12        CONCAT                                           ~16     'Invalid+IPv6+address%3A+', !0
         13        SEND_VAL_EX                                              ~16
         14        DO_FCALL                                      0          
         15      > THROW                                         0          $15
   10    16    >   ASSIGN                                                   !4, 0
   12    17        ISSET_ISEMPTY_CV                                         !2
         18      > JMPZ                                                     ~19, ->49
   13    19    >   TYPE_CHECK                                   16          !2
         20      > JMPZ                                                     ~20, ->34
   14    21    >   CAST                                          4  ~21     !2
         22        ASSIGN                                                   !4, ~21
   15    23        IS_SMALLER                                       ~23     !4, 0
         24      > JMPNZ_EX                                         ~23     ~23, ->27
         25    >   IS_SMALLER                                       ~24     128, !4
         26        BOOL                                             ~23     ~24
         27    > > JMPZ                                                     ~23, ->33
   16    28    >   NEW                                              $25     'InvalidArgumentException'
         29        CONCAT                                           ~26     'Invalid+IPv6+CIDR+mask%3A+', !2
         30        SEND_VAL_EX                                              ~26
         31        DO_FCALL                                      0          
         32      > THROW                                         0          $25
         33    > > JMP                                                      ->47
   19    34    >   BEGIN_SILENCE                                    ~28     
         35        INIT_FCALL                                               'inet_pton'
         36        SEND_VAR                                                 !2
         37        DO_ICALL                                         $29     
         38        END_SILENCE                                              ~28
         39        ASSIGN                                                   !5, $29
   20    40        BOOL_NOT                                         ~31     !5
         41      > JMPZ                                                     ~31, ->47
   21    42    >   NEW                                              $32     'InvalidArgumentException'
         43        CONCAT                                           ~33     'Invalid+IPv6+mask%3A+', !2
         44        SEND_VAL_EX                                              ~33
         45        DO_FCALL                                      0          
         46      > THROW                                         0          $32
   25    47    >   ASSIGN                                                   !6, !1
         48      > JMP                                                      ->72
   27    49    >   INIT_FCALL                                               'explode'
         50        SEND_VAL                                                 '%2F'
         51        SEND_VAR                                                 !1
         52        DO_ICALL                                         $36     
         53        ASSIGN                                                   !7, $36
   28    54        COUNT                                            ~38     !7
         55        ASSIGN                                                   !8, ~38
   30    56        IS_IDENTICAL                                             !8, 1
         57      > JMPZ                                                     ~40, ->60
   31    58    >   ASSIGN                                                   !5, '%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF'
         59      > JMP                                                      ->70
   32    60    >   IS_IDENTICAL                                             !8, 2
         61      > JMPZ                                                     ~42, ->66
   33    62    >   FETCH_DIM_R                                      ~43     !7, 1
         63        CAST                                          4  ~44     ~43
         64        ASSIGN                                                   !4, ~44
         65      > JMP                                                      ->70
   35    66    >   NEW                                              $46     'InvalidArgumentException'
         67        SEND_VAL_EX                                              'Invalid+IPv6+subnet'
         68        DO_FCALL                                      0          
         69      > THROW                                         0          $46
   38    70    >   FETCH_DIM_R                                      ~48     !7, 0
         71        ASSIGN                                                   !6, ~48
   41    72    >   ISSET_ISEMPTY_CV                                 ~50     !5
         73        BOOL_NOT                                         ~51     ~50
         74      > JMPZ                                                     ~51, ->102
   42    75    >   DIV                                              ~52     !4, 16
         76        CAST                                          4  ~53     ~52
         77        ASSIGN                                                   !9, ~53
   43    78        INIT_FCALL                                               'pack'
   44    79        SEND_VAL                                                 'n%2A'
   45    80        INIT_FCALL                                               'array_fill'
         81        SEND_VAL                                                 0
         82        SEND_VAR                                                 !9
         83        SEND_VAL                                                 65535
         84        DO_ICALL                                         $55     
         85        SEND_UNPACK                                              $55
   46    86        INIT_FCALL                                               'array_pad'
         87        MOD                                              ~56     !4, 16
         88        SUB                                              ~57     16, ~56
         89        POW                                              ~58     2, ~57
         90        SUB                                              ~59     ~58, 1
         91        BW_XOR                                           ~60     ~59, 65535
         92        INIT_ARRAY                                       ~61     ~60
         93        SEND_VAL                                                 ~61
         94        SUB                                              ~62     8, !9
         95        SEND_VAL                                                 ~62
         96        SEND_VAL                                                 0
         97        DO_ICALL                                         $63     
         98        SEND_UNPACK                                              $63
         99        CHECK_UNDEF_ARGS                                         
        100        DO_ICALL                                         $64     
   43   101        ASSIGN                                                   !5, $64
   50   102    >   BEGIN_SILENCE                                    ~66     
        103        INIT_FCALL                                               'inet_pton'
        104        SEND_VAR                                                 !6
        105        DO_ICALL                                         $67     
        106        END_SILENCE                                              ~66
        107        ASSIGN                                                   !10, $67
   51   108        BOOL_NOT                                         ~69     !10
        109      > JMPZ                                                     ~69, ->115
   52   110    >   NEW                                              $70     'InvalidArgumentException'
        111        CONCAT                                           ~71     'Invalid+IPv6+address%3A+', !6
        112        SEND_VAL_EX                                              ~71
        113        DO_FCALL                                      0          
        114      > THROW                                         0          $70
   55   115    >   BW_AND                                           ~73     !3, !5
        116        BW_AND                                           ~74     !10, !5
        117        IS_IDENTICAL                                     ~75     ~73, ~74
        118      > RETURN                                                   ~75
   56   119*     > RETURN                                                   null

End of function ipv6_addr_in_subnet

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.16 ms | 1411 KiB | 29 Q