PEGASUS_NAMESPACE_BEGIN static bool _parseLocator( const String &locator, HostAddress& addr, Uint32& port) { const Uint16* first = (const Uint16*)locator.getChar16Data(); const Uint16* last = first + locator.size(); port = HostLocator::PORT_UNSPECIFIED; // Reject zero length locators. if (first == last) { return false; } // Parse the host address. const Uint16* p = first; if (*p == '[') { // Parse "[...]" expresion. const Uint16* start = ++p; while (*p && *p != ']') p++; if (*p != ']') { return false; } addr.setHostAddress(String((const Char16*)start, p - start)); p++; // Only IPV6 addresses may be enclosed in braces. if (addr.getAddressType() != HostAddress::AT_IPV6) { return false; } } else { // Find end-of-string host address (null terminator or colon). const Uint16* start = p; while (*p && *p != ':') p++; addr.setHostAddress(String((const Char16*)start, p - start)); if (!addr.isValid()) { return false; } // IPV6 addresses must be enclosed in braces. if (addr.getAddressType() == HostAddress::AT_IPV6) { return false; } } // Parse the port number: if (*p == ':') { const Uint16* start = ++p; // If empty port number, ignore and proceed as unspecified port. if (start == last) { return true; } port = HostLocator::PORT_INVALID; // Convert string port number to integer (start at end of string). Uint32 r = 1; Uint32 x = 0; for (const Uint16* q = last; q != start; q--) { Uint16 c = q[-1]; if (c > 127 || !isdigit(c)) return false; x += r * (c - '0'); r *= 10; } if (x > HostLocator::MAX_PORT_NUMBER) { return false; } port = x; p++; return true; } else if (*p != '\0') { return false; } // Unreachable! return true; }
void testConstructor_Invalid() { HostAddress testling; CPPUNIT_ASSERT(!testling.isValid()); }