base/win32.cc
const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
  if (size < INET6_ADDRSTRLEN) {
    return NULL;
  }
  const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src);
  int runpos[8];
  int current = 1;
  int max = 0;
  int maxpos = -1;
  int run_array_size = arraysize(runpos);
  // Run over the address marking runs of 0s.
  for (int i = 0; i < run_array_size; ++i) {
    if (as_shorts[i] == 0) {
      runpos[i] = current;
      if (current > max) {
        maxpos = i;
        max = current;
      }
      ++current;
    } else {
      runpos[i] = -1;
      current = 1;
    }
  }

  if (max > 0) {
    int tmpmax = maxpos;
    // Run back through, setting -1 for all but the longest run.
    for (int i = run_array_size - 1; i >= 0; i--) {
      if (i > tmpmax) {
        runpos[i] = -1;
      } else if (runpos[i] == -1) {
        // We're less than maxpos, we hit a -1, so the 'good' run is done.
        // Setting tmpmax -1 means all remaining positions get set to -1.
        tmpmax = -1;
      }
    }
  }

  char* cursor = dst;
  // Print IPv4 compatible and IPv4 mapped addresses using the IPv4 helper.
  // These addresses have an initial run of either eight zero-bytes followed
  // by 0xFFFF, or an initial run of ten zero-bytes.
  if (runpos[0] == 1 && (maxpos == 5 ||
                         (maxpos == 4 && as_shorts[5] == 0xFFFF))) {
    *cursor++ = ':';
    *cursor++ = ':';
    if (maxpos == 4) {
      cursor += rtc::sprintfn(cursor, INET6_ADDRSTRLEN - 2, "ffff:");
    }
    const struct in_addr* as_v4 =
        reinterpret_cast<const struct in_addr*>(&(as_shorts[6]));
    inet_ntop_v4(as_v4, cursor,
                 static_cast<socklen_t>(INET6_ADDRSTRLEN - (cursor - dst)));
  } else {
    for (int i = 0; i < run_array_size; ++i) {
      if (runpos[i] == -1) {
        cursor += rtc::sprintfn(cursor,
                                      INET6_ADDRSTRLEN - (cursor - dst),
                                      "%x", NetworkToHost16(as_shorts[i]));
        if (i != 7 && runpos[i + 1] != 1) {
          *cursor++ = ':';
        }
      } else if (runpos[i] == 1) {
        // Entered the run; print the colons and skip the run.
        *cursor++ = ':';
        *cursor++ = ':';
        i += (max - 1);
      }
    }
  }
  return dst;
}
// Helper function for inet_ntop for IPv6 addresses.
Helper function for inet_ntop for IPv6 addresses.
Helper function for inet_ntop for IPv6 addresses .
const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
  if (size < INET6_ADDRSTRLEN) {
    return NULL;
  }
  const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src);
  int runpos[8];
  int current = 1;
  int max = 0;
  int maxpos = -1;
  int run_array_size = arraysize(runpos);
  for (int i = 0; i < run_array_size; ++i) {
    if (as_shorts[i] == 0) {
      runpos[i] = current;
      if (current > max) {
        maxpos = i;
        max = current;
      }
      ++current;
    } else {
      runpos[i] = -1;
      current = 1;
    }
  }
  if (max > 0) {
    int tmpmax = maxpos;
    for (int i = run_array_size - 1; i >= 0; i--) {
      if (i > tmpmax) {
        runpos[i] = -1;
      } else if (runpos[i] == -1) {
        tmpmax = -1;
      }
    }
  }
  char* cursor = dst;
  if (runpos[0] == 1 && (maxpos == 5 ||
                         (maxpos == 4 && as_shorts[5] == 0xFFFF))) {
    *cursor++ = ':';
    *cursor++ = ':';
    if (maxpos == 4) {
      cursor += rtc::sprintfn(cursor, INET6_ADDRSTRLEN - 2, "ffff:");
    }
    const struct in_addr* as_v4 =
        reinterpret_cast<const struct in_addr*>(&(as_shorts[6]));
    inet_ntop_v4(as_v4, cursor,
                 static_cast<socklen_t>(INET6_ADDRSTRLEN - (cursor - dst)));
  } else {
    for (int i = 0; i < run_array_size; ++i) {
      if (runpos[i] == -1) {
        cursor += rtc::sprintfn(cursor,
                                      INET6_ADDRSTRLEN - (cursor - dst),
                                      "%x", NetworkToHost16(as_shorts[i]));
        if (i != 7 && runpos[i + 1] != 1) {
          *cursor++ = ':';
        }
      } else if (runpos[i] == 1) {
        *cursor++ = ':';
        *cursor++ = ':';
        i += (max - 1);
      }
    }
  }
  return dst;
}
const char * inet_ntop_v6 ( const void * src , char * dst , socklen_t size ) { if ( size < INET6_ADDRSTRLEN ) { return NULL ; } const uint16_t * as_shorts = reinterpret_cast < const uint16_t * > ( src ) ; int runpos [ 8 ] ; int current = 1 ; int max = 0 ; int maxpos = -1 ; int run_array_size = arraysize ( runpos ) ; for ( int i = 0 ; i < run_array_size ; ++ i ) { if ( as_shorts [ i ] == 0 ) { runpos [ i ] = current ; if ( current > max ) { maxpos = i ; max = current ; } ++ current ; } else { runpos [ i ] = -1 ; current = 1 ; } } if ( max > 0 ) { int tmpmax = maxpos ; for ( int i = run_array_size - 1 ; i >= 0 ; i -- ) { if ( i > tmpmax ) { runpos [ i ] = -1 ; } else if ( runpos [ i ] == -1 ) { tmpmax = -1 ; } } } char * cursor = dst ; if ( runpos [ 0 ] == 1 && ( maxpos == 5 || ( maxpos == 4 && as_shorts [ 5 ] == 0xFFFF ) ) ) { * cursor ++ = ' ' ; * cursor ++ = ' ' ; if ( maxpos == 4 ) { cursor += rtc :: sprintfn ( cursor , INET6_ADDRSTRLEN - 2 , " " ) ; } const struct in_addr * as_v4 = reinterpret_cast < const struct in_addr * > ( & ( as_shorts [ 6 ] ) ) ; inet_ntop_v4 ( as_v4 , cursor , static_cast < socklen_t > ( INET6_ADDRSTRLEN - ( cursor - dst ) ) ) ; } else { for ( int i = 0 ; i < run_array_size ; ++ i ) { if ( runpos [ i ] == -1 ) { cursor += rtc :: sprintfn ( cursor , INET6_ADDRSTRLEN - ( cursor - dst ) , " " , NetworkToHost16 ( as_shorts [ i ] ) ) ; if ( i != 7 && runpos [ i + 1 ] != 1 ) { * cursor ++ = ' ' ; } } else if ( runpos [ i ] == 1 ) { * cursor ++ = ' ' ; * cursor ++ = ' ' ; i += ( max - 1 ) ; } } } return dst ; }
Helper function for inet_ntop for IPv6 addresses.
Helper function for inet_ntop for IPv6 addresses .
// Run over the address marking runs of 0s. // Run back through, setting -1 for all but the longest run. // We're less than maxpos, we hit a -1, so the 'good' run is done. // Setting tmpmax -1 means all remaining positions get set to -1. // Print IPv4 compatible and IPv4 mapped addresses using the IPv4 helper. // These addresses have an initial run of either eight zero-bytes followed // by 0xFFFF, or an initial run of ten zero-bytes. // Entered the run; print the colons and skip the run.