Home » Networking » Net_IPv6 » Bug #3405
compress() does not compress '0000'
Details
| Submitted | 2005-02-08 15:38 UTC |
|---|---|
| From | elfrink at introweb dot nl |
| Assigned | alexmerz |
| Status | Closed |
| Package | Net_IPv6 |
| PHP Version | 4.3.10 |
| OS | FreeBSD |
| Roadmaps | (Not assigned) |
Comments
[2005-02-08 15:38 UTC] elfrink at introweb dot nl
Description:
------------
compress() does not compress IPv6 address that have '0000' in them.
Reproduce code:
---------------
require("Net/IPv6.php");
echo Net_IPv6::compress("2010:0588:0000:faef:1428:0000:0000:57ab");
Expected result:
----------------
2010:0588:0000:faef:1428::57ab
Actual result:
--------------
2010:0588:0000:faef:1428:000:000:57ab
[2005-02-08 15:39 UTC] elfrink at introweb dot nl
Fix:
--- Net/IPv6.php Tue Feb 8 16:27:36 2005
+++ ../IPv6.php Tue Feb 8 15:25:56 2005
@@ -116,7 +116,7 @@
$ipp = explode(':',$ip);
for($i=0; $i<count($ipp); $i++) {
- $ipp[$i] = $ipp[$i];
+ $ipp[$i] = dechex(hexdec($ipp[$i]));
if(hexdec($ipp[$i])>0) {
$ipp[$i]=$ipp[$i].'_';
}
[2005-02-10 08:22 UTC] alexmerz at php dot net
I will check the fix and submit it to CVS during the weekend. Thank you.
[2005-09-01 04:53 UTC] iqtest at afraid dot org
Having trouble, here's some examples:
2001:0760:0202:f265:0001:0002:0003:0025 compresses to
2:01:760:202:f265:1:2:3:25
2001:0618:0400:1c85:0999:0999:0999:0999 doesn't seem to compress at all
3ffe:02e0:0123:0123:0123:0123:0123:0123 compresses to
3ffe:2e::123:123:123:123:123:123
fe80:0000:0250:8dff:0001:0002:0003:0004 compresses to
fe8::0:250:8dff:1:2:3:4
Josh
[2005-09-01 07:59 UTC] elfrink at introweb dot nl
That's a completely different bug. But it's fixed by this almost complete rewrite of Compress():
--- IPv6.php.orig Thu Sep 1 08:54:46 2005
+++ IPv6.php Thu Sep 1 09:52:33 2005
@@ -115,37 +115,22 @@
*/
function Compress($ip) {
$cip = $ip;
- if (!strstr($ip, "::") && strstr($ip, '0:0')) {
+ if (!strstr($ip, '::')) {
$ipp = explode(':',$ip);
for($i=0; $i<count($ipp); $i++) {
$ipp[$i] = dechex(hexdec($ipp[$i]));
- if(hexdec($ipp[$i])>0) {
- $ipp[$i]=$ipp[$i].'_';
- }
- }
- $cip = join(':',$ipp);
- $stop = false;
- $pattern = "0:0";
- $pos=-1;
-
- while(!$stop) {
- $pos = strpos($cip, $pattern);
- if($pos === false) {
- $stop = true;
- $pos = -1;
- } else {
- $pattern = $pattern.":0";
- }
- }
-
-
- $cip = preg_replace("/".substr($pattern,0,-2)."/", ':', $cip,1);
- if(1!=strlen($cip)) {
- $cip = str_replace(':::', '::', $cip);
- $cip = str_replace('_', '', $cip);
- } else {
- $cip = "::";
}
+ $cip = ':' . join(':',$ipp) . ':';
+ preg_match_all("/(:0)+/", $cip, $zeros);
+ if (count($zeros[0])>0) {
+ $match = '';
+ foreach($zeros[0] as $zero) {
+ if (strlen($zero) > strlen($match))
+ $match = $zero;
+ }
+ $cip = preg_replace('/' . $match . '/', ':', $cip, 1);
+ }
+ $cip = preg_replace('/((^:)|(:$))/', '' ,$cip);
}
return $cip;
[2005-09-01 08:29 UTC] elfrink at introweb dot nl
Oops, needs an extra preg_replace() to account for :: at the beginning or the end of the address.
--- IPv6.php.orig Thu Sep 1 10:26:08 2005
+++ IPv6.php Thu Sep 1 10:26:15 2005
@@ -131,6 +131,7 @@
$cip = preg_replace('/' . $match . '/', ':', $cip, 1);
}
$cip = preg_replace('/((^:)|(:$))/', '' ,$cip);
+ $cip = preg_replace('/((^:)|(:$))/', '::' ,$cip);
}
return $cip;