- /* IPv6 */
-
- char *last_colon = strrchr(str, ':');
-
- if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
- /* Dotted quad suffix notation, convert to standard IPv6 notation */
- for(int i = 0; i < 4; i++)
- if(x[i] > 255) {
- return false;
- }
-
- snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
- }
-
- char *double_colon = strstr(str, "::");
-
- if(double_colon) {
- /* Figure out how many zero groups we need to expand */
- int zero_group_count = 8;
-
- for(const char *cur = str; *cur; cur++)
- if(*cur != ':') {
- zero_group_count--;
-
- while(cur[1] && cur[1] != ':') {
- cur++;
- }
- }
-
- if(zero_group_count < 1) {
- return false;
- }
-
- /* Split the double colon in the middle to make room for zero groups */
- double_colon++;
- memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
-
- /* Write zero groups in the resulting gap, overwriting the second colon */
- for(int i = 0; i < zero_group_count; i++) {
- memcpy(&double_colon[i * 2], "0:", 2);
- }
-
- /* Remove any leading or trailing colons */
- if(str[0] == ':') {
- memmove(&str[0], &str[1], strlen(&str[1]) + 1);
- }
-
- if(str[strlen(str) - 1] == ':') {
- str[strlen(str) - 1] = 0;
- }
- }
-
- if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
- &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {