--- djbdns-1.05.orig/ip6_fmt.c 2006-06-20 15:50:55.000000000 +0900 +++ djbdns-1.05/ip6_fmt.c 2008-03-08 18:08:24.000000000 +0900 @@ -9,43 +9,43 @@ unsigned int ip6_fmt(char *s,const char ip[16]) { unsigned int len; - unsigned int i; - unsigned int temp; - unsigned int compressing; + unsigned int i, j, k; + unsigned long u[16]; unsigned int compressed; - int j; + unsigned int z; - len = 0; compressing = 0; compressed = 0; - for (j=0; j<16; j+=2) { - if (j==12 && ip6_isv4mapped(ip)) { - temp=ip4_fmt(s,ip+12); - len+=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 && !compressed) { - if (!compressing) { - compressing=1; - if (j==0) { - if (s) *s++=':'; ++len; - } - } - } else { - if (compressing) { - compressing=0; ++compressed; - if (s) *s++=':'; ++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; } - i = fmt_xlong(s,temp); len += i; if (s) s += i; - if (j<14) { - if (s) *s++ = ':'; - ++len; + *s++ = ':'; ++len; + compressed = 1; k = z; + } + + for (; k <= z; k++) { + if (len) { + *s++ = ':'; ++len; } + i = fmt_xlong(s, u[k]); len += i; if (s) s += i; } } - if (compressing) { *s++=':'; ++len; } - -/* if (s) *s=0; */ return len; }