// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. #include #include #include #include #include #include #include #include #include #include namespace isc { namespace dns { namespace rdata { namespace generic { namespace detail { namespace { // Convert a DDD form to the corresponding integer int decimalToNumber(const char* s, const char* s_end) { if (s_end - s < 3) { isc_throw(InvalidRdataText, "Escaped digits too short"); } const std::string num_str(s, s + 3); try { const int i = boost::lexical_cast(num_str); if (i > 255) { isc_throw(InvalidRdataText, "Escaped digits too large: " << num_str); } return (i); } catch (const boost::bad_lexical_cast&) { isc_throw(InvalidRdataText, "Invalid form for escaped digits: " << num_str); } } } void stringToCharString(const MasterToken::StringRegion& str_region, CharString& result) { // make a space for the 1-byte length field; filled in at the end result.push_back(0); bool escape = false; const char* s = str_region.beg; const char* const s_end = str_region.beg + str_region.len; for (size_t n = str_region.len; n != 0; --n, ++s) { int c = (*s & 0xff); if (escape && std::isdigit(c) != 0) { c = decimalToNumber(s, s_end); assert(n >= 3); n -= 2; s += 2; } else if (!escape && c == '\\') { escape = true; continue; } escape = false; result.push_back(c); } if (escape) { // terminated by non-escaped '\' isc_throw(InvalidRdataText, "character-string ends with '\\'"); } if (result.size() > MAX_CHARSTRING_LEN + 1) { // '+ 1' due to the len field isc_throw(CharStringTooLong, "character-string is too long: " << (result.size() - 1) << "(+1) characters"); } result[0] = result.size() - 1; } std::string charStringToString(const CharString& char_string) { std::string s; for (CharString::const_iterator it = char_string.begin() + 1; it != char_string.end(); ++it) { const uint8_t ch = *it; if ((ch < 0x20) || (ch >= 0x7f)) { // convert to escaped \xxx (decimal) format s.push_back('\\'); s.push_back('0' + ((ch / 100) % 10)); s.push_back('0' + ((ch / 10) % 10)); s.push_back('0' + (ch % 10)); continue; } if ((ch == '"') || (ch == ';') || (ch == '\\')) { s.push_back('\\'); } s.push_back(ch); } return (s); } } // end of detail } // end of generic } // end of rdata } // end of dns } // end of isc