3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); class StackOverflowCopyPaste { /** @var int $v4MaskBits */ private $v4MaskBits; /** @var int $v6MaskBits */ private $v6MaskBits; /** * @param int $v4MaskBits * @param int $v6MaskBits */ public function __construct(int $v4MaskBits = 24, int $v6MaskBits = 48) { $this->v4MaskBits = $v4MaskBits; $this->v6MaskBits = $v6MaskBits; } /** * Return the given subnet for an IP and the configured mask bits * * Determine if the IP is an IPv4 or IPv6 address, then pass to the correct * method for handling that specific type. * * @param string $ip * @return string */ public function getSubnet(string $ip): string { if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) { return $this->getIPv4Subnet( $ip, (int) ($this->v4MaskBits ?? 32) ); } return $this->getIPv6Subnet( $ip, (int) ($this->v6MaskBits ?? 128) ); } /** * Return the given subnet for an IPv4 address and mask bits * * @param string $ip * @param int $maskBits * @return string */ public function getIPv4Subnet(string $ip, int $maskBits = 32): string { $binary = \inet_pton($ip); for ($i = 32; $i > $maskBits; $i -= 8) { $j = \intdiv($i, 8) - 1; $k = (int) \min(8, $i - $maskBits); $mask = (0xff - ((1 << $k) - 1)); $int = \unpack('C', $binary[$j]); $binary[$j] = \pack('C', $int[1] & $mask); } return \inet_ntop($binary).'/'.$maskBits; } /** * Return the given subnet for an IPv6 address and mask bits * * @param string $ip * @param int $maskBits * @return string */ public function getIPv6Subnet(string $ip, int $maskBits = 48): string { $binary = \inet_pton($ip); for ($i = 128; $i > $maskBits; $i -= 8) { $j = \intdiv($i, 8) - 1; $k = (int) \min(8, $i - $maskBits); $mask = (0xff - ((1 << $k) - 1)); $int = \unpack('C', $binary[$j]); $binary[$j] = \pack('C', $int[1] & $mask); } return \inet_ntop($binary).'/'.$maskBits; } } $mask = new StackOverflowCopyPaste(24, 48); var_dump($mask->getSubnet('12.34.56.78')); var_dump($mask->getSubnet('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i2ufm
function name:  (null)
number of ops:  18
compiled vars:  !0 = $mask
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   NEW                                              $1      'StackOverflowCopyPaste'
          1        SEND_VAL_EX                                              24
          2        SEND_VAL_EX                                              48
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
   87     5        INIT_FCALL                                               'var_dump'
          6        INIT_METHOD_CALL                                         !0, 'getSubnet'
          7        SEND_VAL_EX                                              '12.34.56.78'
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR                                                 $4
         10        DO_ICALL                                                 
   88    11        INIT_FCALL                                               'var_dump'
         12        INIT_METHOD_CALL                                         !0, 'getSubnet'
         13        SEND_VAL_EX                                              '2001%3A0db8%3A85a3%3A0000%3A0000%3A8a2e%3A0370%3A7334'
         14        DO_FCALL                                      0  $6      
         15        SEND_VAR                                                 $6
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Class StackOverflowCopyPaste:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i2ufm
function name:  __construct
number of ops:  7
compiled vars:  !0 = $v4MaskBits, !1 = $v6MaskBits
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV_INIT                                        !0      24
          1        RECV_INIT                                        !1      48
   17     2        ASSIGN_OBJ                                               'v4MaskBits'
          3        OP_DATA                                                  !0
   18     4        ASSIGN_OBJ                                               'v6MaskBits'
          5        OP_DATA                                                  !1
   19     6      > RETURN                                                   null

End of function __construct

Function getsubnet:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i2ufm
function name:  getSubnet
number of ops:  28
compiled vars:  !0 = $ip
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%5E%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%24%2F'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5      > JMPZ                                                     $1, ->16
   33     6    >   INIT_METHOD_CALL                                         'getIPv4Subnet'
   34     7        SEND_VAR_EX                                              !0
   35     8        FETCH_OBJ_IS                                     ~2      'v4MaskBits'
          9        COALESCE                                         ~3      ~2
         10        QM_ASSIGN                                        ~3      32
         11        CAST                                          4  ~4      ~3
         12        SEND_VAL_EX                                              ~4
         13        DO_FCALL                                      0  $5      
         14        VERIFY_RETURN_TYPE                                       $5
         15      > RETURN                                                   $5
   38    16    >   INIT_METHOD_CALL                                         'getIPv6Subnet'
   39    17        SEND_VAR_EX                                              !0
   40    18        FETCH_OBJ_IS                                     ~6      'v6MaskBits'
         19        COALESCE                                         ~7      ~6
         20        QM_ASSIGN                                        ~7      128
         21        CAST                                          4  ~8      ~7
         22        SEND_VAL_EX                                              ~8
         23        DO_FCALL                                      0  $9      
         24        VERIFY_RETURN_TYPE                                       $9
         25      > RETURN                                                   $9
   42    26*       VERIFY_RETURN_TYPE                                       
         27*     > RETURN                                                   null

End of function getsubnet

Function getipv4subnet:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 8
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 8
Branch analysis from position: 42
Branch analysis from position: 8
filename:       /in/i2ufm
function name:  getIPv4Subnet
number of ops:  51
compiled vars:  !0 = $ip, !1 = $maskBits, !2 = $binary, !3 = $i, !4 = $j, !5 = $k, !6 = $mask, !7 = $int
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      32
   53     2        INIT_FCALL                                               'inet_pton'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !2, $8
   54     6        ASSIGN                                                   !3, 32
          7      > JMP                                                      ->40
   55     8    >   INIT_FCALL                                               'intdiv'
          9        SEND_VAR                                                 !3
         10        SEND_VAL                                                 8
         11        DO_ICALL                                         $11     
         12        SUB                                              ~12     $11, 1
         13        ASSIGN                                                   !4, ~12
   56    14        INIT_FCALL                                               'min'
         15        SEND_VAL                                                 8
         16        SUB                                              ~14     !3, !1
         17        SEND_VAL                                                 ~14
         18        DO_ICALL                                         $15     
         19        CAST                                          4  ~16     $15
         20        ASSIGN                                                   !5, ~16
   57    21        SL                                               ~18     1, !5
         22        SUB                                              ~19     ~18, 1
         23        SUB                                              ~20     255, ~19
         24        ASSIGN                                                   !6, ~20
   58    25        INIT_FCALL                                               'unpack'
         26        SEND_VAL                                                 'C'
         27        FETCH_DIM_R                                      ~22     !2, !4
         28        SEND_VAL                                                 ~22
         29        DO_ICALL                                         $23     
         30        ASSIGN                                                   !7, $23
   59    31        INIT_FCALL                                               'pack'
         32        SEND_VAL                                                 'C'
         33        FETCH_DIM_R                                      ~26     !7, 1
         34        BW_AND                                           ~27     !6, ~26
         35        SEND_VAL                                                 ~27
         36        DO_ICALL                                         $28     
         37        ASSIGN_DIM                                               !2, !4
         38        OP_DATA                                                  $28
   54    39        ASSIGN_OP                                     2          !3, 8
         40    >   IS_SMALLER                                               !1, !3
         41      > JMPNZ                                                    ~30, ->8
   61    42    >   INIT_FCALL                                               'inet_ntop'
         43        SEND_VAR                                                 !2
         44        DO_ICALL                                         $31     
         45        CONCAT                                           ~32     $31, '%2F'
         46        CONCAT                                           ~33     ~32, !1
         47        VERIFY_RETURN_TYPE                                       ~33
         48      > RETURN                                                   ~33
   62    49*       VERIFY_RETURN_TYPE                                       
         50*     > RETURN                                                   null

End of function getipv4subnet

Function getipv6subnet:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 8
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 8
Branch analysis from position: 42
Branch analysis from position: 8
filename:       /in/i2ufm
function name:  getIPv6Subnet
number of ops:  51
compiled vars:  !0 = $ip, !1 = $maskBits, !2 = $binary, !3 = $i, !4 = $j, !5 = $k, !6 = $mask, !7 = $int
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      48
   73     2        INIT_FCALL                                               'inet_pton'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !2, $8
   74     6        ASSIGN                                                   !3, 128
          7      > JMP                                                      ->40
   75     8    >   INIT_FCALL                                               'intdiv'
          9        SEND_VAR                                                 !3
         10        SEND_VAL                                                 8
         11        DO_ICALL                                         $11     
         12        SUB                                              ~12     $11, 1
         13        ASSIGN                                                   !4, ~12
   76    14        INIT_FCALL                                               'min'
         15        SEND_VAL                                                 8
         16        SUB                                              ~14     !3, !1
         17        SEND_VAL                                                 ~14
         18        DO_ICALL                                         $15     
         19        CAST                                          4  ~16     $15
         20        ASSIGN                                                   !5, ~16
   77    21        SL                                               ~18     1, !5
         22        SUB                                              ~19     ~18, 1
         23        SUB                                              ~20     255, ~19
         24        ASSIGN                                                   !6, ~20
   78    25        INIT_FCALL                                               'unpack'
         26        SEND_VAL                                                 'C'
         27        FETCH_DIM_R                                      ~22     !2, !4
         28        SEND_VAL                                                 ~22
         29        DO_ICALL                                         $23     
         30        ASSIGN                                                   !7, $23
   79    31        INIT_FCALL                                               'pack'
         32        SEND_VAL                                                 'C'
         33        FETCH_DIM_R                                      ~26     !7, 1
         34        BW_AND                                           ~27     !6, ~26
         35        SEND_VAL                                                 ~27
         36        DO_ICALL                                         $28     
         37        ASSIGN_DIM                                               !2, !4
         38        OP_DATA                                                  $28
   74    39        ASSIGN_OP                                     2          !3, 8
         40    >   IS_SMALLER                                               !1, !3
         41      > JMPNZ                                                    ~30, ->8
   81    42    >   INIT_FCALL                                               'inet_ntop'
         43        SEND_VAR                                                 !2
         44        DO_ICALL                                         $31     
         45        CONCAT                                           ~32     $31, '%2F'
         46        CONCAT                                           ~33     ~32, !1
         47        VERIFY_RETURN_TYPE                                       ~33
         48      > RETURN                                                   ~33
   82    49*       VERIFY_RETURN_TYPE                                       
         50*     > RETURN                                                   null

End of function getipv6subnet

End of class StackOverflowCopyPaste.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.41 ms | 1413 KiB | 29 Q