void IPv4AddressToString(IPv4Address address, std::u16string& buffer)
    {
        const char* const InvalidIPv4AddressStr = "Invalid IPv4 address!";
        const auto& addressBytes = address.Bytes;

        const in_addr addr = { static_cast<uchar>(addressBytes[0]), static_cast<uchar>(addressBytes[1]),
            static_cast<uchar>(addressBytes[2]), static_cast<uchar>(addressBytes[3]) };
        const auto& ntFunctions = GetNtFunctions();

        //If the length of the buffer pointed to by the AddressString parameter is not large enough 
        //to receive the string representation of the IPv4 address and port, RtlIpv4AddressToStringEx 
        //returns ERROR_INVALID_PARAMETER and sets the AddressStringLength parameter to the buffer length required. 

        ulong bufferLength = {};
        auto errCode = ntFunctions.RtlIpv4AddressToStringEx(&addr, 0, nullptr, &bufferLength);

        //如果 bufferLength 在调用后依旧为 0 说明 address 有问题。
        Try<std::invalid_argument>(bufferLength == 0, InvalidIPv4AddressStr);

        buffer.resize(static_cast<std::size_t>(bufferLength));
        errCode = ntFunctions.RtlIpv4AddressToStringEx(&addr, 0, reinterpret_cast<wchar_t*>(&buffer[0]), &bufferLength);
        
        //如果有错误,前面必然已经抛出异常,故这里必然是 Success。
        assert(errCode == NtFunctions::NtSuccess);
        assert(bufferLength > 0);

        //在写入的 buffer 中包含 '\0',会导致判等出现奇怪的结果,故舍弃掉结尾的空字符。
        buffer.pop_back();
    }
Example #2
0
void LineEdit::IntValidator::assign(std::u16string &string,
                                    const std::u16string &arg) const {
  if(arg.size()==0){
    if(string.size()==0){
      string.resize(1);
      string[0] = '0';
      }
    return;
    }

  bool good=true;
  if(arg[0]=='-'){
    if(arg.size()!=1){
      if(arg[1]=='0' && arg.size()!=2)
        good=false;
      for(size_t i=1; good && i<arg.size(); ++i ){
        const char16_t c = arg[i];
        good &= ('0'<=c && c<='9');
        }
      } else {
      good = false;
      }
    } else {
    if(arg[0]=='0' && arg.size()!=1)
      good=false;
    for(size_t i=0; good && i<arg.size(); ++i ){
      const char16_t c = arg[i];
      good &= ('0'<=c && c<='9');
      }
    }

  if(good){
    string = arg;
    } else {
    if(string.size()==0){
      string.resize(1);
      string[0] = '0';
      }
    }
  }
    void IPv6AddressToString(IPv6Address address, std::uint32_t scopeId, std::u16string& buffer)
    {
        const char* const InvalidIPv6AddressStr = "Invalid IPv6 address!";
        ::in6_addr v6Addr;
        auto& desAddrBytes = v6Addr.s6_addr;
        auto& srcAddrBytes = address.Bytes;
        for (std::size_t i = 0; i < kIPv4AddressBytes; ++i)
            desAddrBytes[i] = static_cast<uchar>(srcAddrBytes[i]);

        const auto& ntFunctions = GetNtFunctions();
        ulong bufferLength = {};

        auto errCode = ntFunctions.RtlIpv6AddressToStringEx(&v6Addr, static_cast<ulong>(scopeId), 0, nullptr, &bufferLength);
        Try<std::invalid_argument>(bufferLength != 0, InvalidIPv6AddressStr);

        buffer.resize(static_cast<std::size_t>(bufferLength));
        errCode = ntFunctions.RtlIpv6AddressToStringEx(&v6Addr, static_cast<ulong>(scopeId), 0, reinterpret_cast<wchar_t*>(&buffer[0]), &bufferLength);

        assert(errCode == NtFunctions::NtSuccess);

        buffer.pop_back();
    }