13#define BITSUBSTITUTION
24 char* hexdigits =
"0123456789ABCDEF";
30 number = decimal / 16;
33 hex[0] = hexdigits[number];
34 hex[1] = hexdigits[rest];
37static char strnum[FMT_ULONG];
48int ip6_bitstring(stralloc *ip6string,
char *ip6addr,
unsigned int prefix)
51 int bit, octettbitpos, number, shiftedvalue;
52 int i, slashpos, ip6len;
55 char subvalueforbitone[1];
56 subvalueforbitone[0] = 96;
59 ip6len = str_len(ip6addr);
60 slashpos = byte_chr(ip6addr,ip6len,
'/');
61 if (!stralloc_copyb(ip6string,ip6addr,slashpos))
return -1;
62 ip6addr[slashpos] =
'\0';
64 if (!ip6_scan(ip6addr,ip6))
return 1;
65 if (!stralloc_copys(ip6string,
""))
return -1;
67 for (i = 0; i < 16; i++) {
68 number = (
unsigned char) ip6[i];
70 for (octettbitpos = 7; octettbitpos >= 0; octettbitpos--) {
71 shiftedvalue = 1 << octettbitpos;
72 bit = number / shiftedvalue;
73 number = number - bit * (shiftedvalue);
77 if (!stralloc_catb(ip6string,subvalueforbitone,1))
return -1;
78 subvalueforbitone[0]++;
80 if (!stralloc_cats(ip6string,
"1"))
return -1;
83 if (!stralloc_cats(ip6string,
"0"))
return -1;
86 if (prefix == 0)
return 0;
106 int len, prefix, shiftedvalue;
108 int decimalnumber = 0;
110 char ip6compact[40] = {0};
112 if (!stralloc_copys(ip6addr,
""))
return -1;
113 prefix = ip6string->len - 1;
115 if (prefix <= 0)
return 1;
116 if (prefix <= 1 || prefix > 128)
return 1;
118 if (ip6string->s[0] ==
'^') j = 1;
120 for (i = j, j = 0; i <= prefix; i++) {
121 if (ip6string->s[i] !=
'0') {
122 shiftedvalue = 1 << bitpos;
123 decimalnumber += shiftedvalue;
128 ip6[j] = (
unsigned char) decimalnumber;
137 ip6[j] = (
unsigned char) decimalnumber;
141 len = ip6_fmt(ip6compact,ip6);
144 if (!stralloc_copyb(ip6addr,ip6compact,len))
return -1;
145 if (!stralloc_cats(ip6addr,
"/"))
return -1;
146 if (!stralloc_catb(ip6addr,strnum,fmt_ulong(strnum,prefix)))
return -1;
147 if (!stralloc_0(ip6addr))
return -1;
165 char hexvalue[3] = {0, 0, 0};
168 if (!stralloc_copys(&addr,src))
return -1;
169 if (!stralloc_0(&addr))
return -1;
171 if (ip6_scan(addr.s,ip6) == 0)
return 1;
172 if (!stralloc_copys(dest,
""))
return -1;
174 for (i = 0; i < 16; i++) {
175 bytetohex((
unsigned char)ip6[i],hexvalue);
176 stralloc_catb(dest,hexvalue,2);
177 if (!((i+1) % 2) && (i+1) < 16)
178 if (!stralloc_cats(dest,
":"))
return -1;
int bitstring_ip6(stralloc *ip6addr, stralloc *ip6string)
void bytetohex(unsigned char decimal, char hex[3])
int ip6_bitstring(stralloc *ip6string, char *ip6addr, unsigned int prefix)
unsigned int ip6_fmt_str(stralloc *dest, char *src)