--- ucspi-tcp-0.88.orig/ip6_fmt.c 2008-03-08 17:53:53.000000000 +0900 +++ ucspi-tcp-0.88/ip6_fmt.c 2008-03-08 18:00:37.000000000 +0900 @@ -6,39 +6,43 @@ unsigned int ip6_fmt(char *s,char ip[16]) { unsigned int len; - unsigned int i; - unsigned int temp; - unsigned int compressing; - int j; + unsigned int i, j, k; + unsigned long u[16]; + unsigned int compressed; + unsigned int z; - len = 0; compressing = 0; - for (j=0; j<16; j+=2) { - if (j==12 && ip6_isv4mapped(ip)) { - temp=ip4_fmt(s,ip+12); - len+=temp; - s+=temp; + len = 0; compressed = 0; + + for (j = 0; j < 16;) { + if (j == 12 && ip6_isv4mapped(ip)) { + *s++ = ':'; ++len; + i = ip4_fmt(s,ip + 12); len += i; if (s) s += i; break; } - temp = ((unsigned long) (unsigned char) ip[j] << 8) + - (unsigned long) (unsigned char) ip[j+1]; - if (temp == 0) { - if (!compressing) { - compressing=1; - if (j==0) { - *s=':'; s+=1; ++len; - } + k = 0; z = -1; + do { + u[++z] = ((unsigned long) (unsigned char) ip[j] << 8) + + (unsigned long) (unsigned char) ip[j+1]; + j += 2; + } while (!u[z] && j < 16); + + if (!compressed && z > 1) { + if (!u[z]) { + *s++ = ':'; ++len; + *s++ = ':'; ++len; + break; } - } else { - if (compressing) { - compressing=0; - *s=':'; s+=1; ++len; + *s++ = ':'; ++len; + compressed = 1; k = z; + } + + for (; k <= z; k++) { + if (len) { + *s++ = ':'; ++len; } - i = fmt_xlong(s,temp); len += i; if (s) s += i; - if (s && j<14) *s++ = ':'; ++len; + i = fmt_xlong(s, u[k]); len += i; if (s) s += i; } } - - if (s) *s=0; return len; }