@@ -194,37 +194,25 @@ public function cidrToIpv6($cidr)
194
194
195
195
list ($ ip , $ range ) = explode ('/ ' , $ cidr );
196
196
197
- // Convert the IPv6 into binary
198
- $ binFirstAddress = inet_pton ($ ip );
197
+ $ parts = explode (': ' , $ this ->expandIpv6 ($ ip ));
199
198
200
- // Convert the binary string to a string with hexadecimal characters
201
- $ hexStartAddress = @ reset (@ unpack ( ' H* 0 ' , $ binFirstAddress ) );
199
+ $ bitStart = str_repeat ( ' 1 ' , $ range ) . str_repeat ( ' 0 ' , 128 - $ range );
200
+ $ bitEnd = str_repeat ( ' 0 ' , $ range ) . str_repeat ( ' 1 ' , 128 - $ range );
202
201
203
- // Get available bits
204
- $ bits = 128 - $ range ;
202
+ $ floors = str_split ( $ bitStart , 16 );
203
+ $ ceilings = str_split ( $ bitEnd , 16 ) ;
205
204
206
- $ hexLastAddress = $ hexStartAddress ;
205
+ $ start = [];
206
+ $ end = [];
207
207
208
- $ pos = 31 ;
209
- while ($ bits > 0 ) {
210
- // Convert current character to an integer
211
- $ int = hexdec (substr ($ hexLastAddress , $ pos , 1 ));
212
-
213
- // Convert it back to a hexadecimal character
214
- $ new = dechex ($ int | (pow (2 , min (4 , $ bits )) - 1 ));
215
-
216
- // And put that character back in the string
217
- $ hexLastAddress = substr_replace ($ hexLastAddress , $ new , $ pos , 1 );
218
-
219
- $ bits -= 4 ;
220
- --$ pos ;
208
+ for ($ i = 0 ; $ i < 8 ; ++$ i ) {
209
+ $ start [] = dechex (hexdec ($ parts [$ i ]) & hexdec (base_convert ($ floors [$ i ], 2 , 16 ))) . ': ' ;
210
+ $ end [] = dechex (hexdec ($ parts [$ i ]) | hexdec (base_convert ($ ceilings [$ i ], 2 , 16 ))) . ': ' ;
221
211
}
222
212
223
- $ binLastAddress = pack ('H* ' , $ hexLastAddress );
224
-
225
213
return [
226
- 'ip_start ' => $ this ->expand ( inet_ntop ( $ binFirstAddress )),
227
- 'ip_end ' => $ this ->expand ( inet_ntop ( $ binLastAddress )),
214
+ 'ip_start ' => $ this ->expandIpv6 ( substr ( implode ( '' , $ start ), 0 , - 1 )),
215
+ 'ip_end ' => $ this ->expandIpv6 ( substr ( implode ( '' , $ end ), 0 , - 1 )),
228
216
];
229
217
}
230
218
0 commit comments