name.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cctype>
  15. #include <cassert>
  16. #include <iterator>
  17. #include <functional>
  18. #include <vector>
  19. #include <iostream>
  20. #include <algorithm>
  21. #include <util/buffer.h>
  22. #include <dns/exceptions.h>
  23. #include <dns/name.h>
  24. #include <dns/name_internal.h>
  25. #include <dns/messagerenderer.h>
  26. using namespace std;
  27. using namespace isc::util;
  28. using isc::dns::NameComparisonResult;
  29. using namespace isc::dns::name::internal;
  30. namespace isc {
  31. namespace dns {
  32. namespace {
  33. ///
  34. /// These are shortcut arrays for efficient character conversion.
  35. /// digitvalue converts a digit character to the corresponding integer.
  36. /// maptolower convert uppercase alphabets to their lowercase counterparts.
  37. /// We once used a helper non-local static object to avoid hardcoding the
  38. /// array members, but we then realized it's susceptible to static
  39. /// initialization order fiasco: Since these constants are used in a Name
  40. /// constructor, a non-local static Name object defined in another translation
  41. /// unit than this file may not be initialized correctly.
  42. /// There are several ways to address this issue, but in this specific case
  43. /// we chose the naive but simple hardcoding approach.
  44. ///
  45. /// These definitions are derived from BIND 9's libdns module.
  46. /// Note: we could use the standard tolower() function instead of the
  47. /// maptolower array, but a benchmark indicated that the private array could
  48. /// improve the performance of message rendering (which internally uses the
  49. /// array heavily) about 27%. Since we want to achieve very good performance
  50. /// for message rendering in some cases, we'll keep using it.
  51. const char digitvalue[256] = {
  52. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16
  53. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32
  54. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 48
  55. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 64
  56. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80
  57. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96
  58. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112
  59. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128
  60. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  61. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  62. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  65. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  66. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  67. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 256
  68. };
  69. }
  70. namespace name {
  71. namespace internal {
  72. const uint8_t maptolower[] = {
  73. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  74. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  75. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  76. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  77. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  78. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  79. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  80. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  81. 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, // ..., 'A' - 'G'
  82. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, // 'H' - 'O'
  83. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, // 'P' - 'W'
  84. 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, // 'X' - 'Z', ...
  85. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  86. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  87. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  88. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  89. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  90. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  91. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  92. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  93. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  94. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  95. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  96. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  97. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  98. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  99. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  100. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  101. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  102. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  103. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  104. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  105. };
  106. } // end of internal
  107. } // end of name
  108. namespace {
  109. ///
  110. /// Textual name parser states.
  111. ///
  112. typedef enum {
  113. ft_init = 0, // begin of the name
  114. ft_start, // begin of a label
  115. ft_ordinary, // parsing an ordinary label
  116. ft_initialescape, // just found '\'
  117. ft_escape, // begin of handling a '\'-escaped sequence
  118. ft_escdecimal, // parsing a '\DDD' octet.
  119. // Unused at this moment. We'll revisit this when we support master file
  120. // parser where @ is used to mean an origin name.
  121. ft_at
  122. } ft_state;
  123. }
  124. Name::Name(const std::string &namestring, bool downcase) {
  125. //
  126. // Initialize things to make the compiler happy; they're not required.
  127. //
  128. unsigned int digits = 0;
  129. unsigned int value = 0;
  130. unsigned int count = 0;
  131. //
  132. // Set up the state machine.
  133. //
  134. std::string::const_iterator s = namestring.begin();
  135. std::string::const_iterator send = namestring.end();
  136. bool done = false;
  137. bool is_root = false;
  138. ft_state state = ft_init;
  139. NameOffsets offsets;
  140. offsets.reserve(Name::MAX_LABELS);
  141. offsets.push_back(0);
  142. NameString ndata;
  143. ndata.reserve(Name::MAX_WIRE);
  144. // should we refactor this code using, e.g, the state pattern? Probably
  145. // not at this point, as this is based on proved code (derived from BIND9)
  146. // and it's less likely that we'll have more variations in the domain name
  147. // syntax. If this ever happens next time, we should consider refactor
  148. // the code, rather than adding more states and cases below.
  149. while (ndata.size() < Name::MAX_WIRE && s != send && !done) {
  150. unsigned char c = *s++;
  151. switch (state) {
  152. case ft_init:
  153. //
  154. // Is this the root name?
  155. //
  156. if (c == '.') {
  157. if (s != send) {
  158. isc_throw(EmptyLabel,
  159. "non terminating empty label in " << namestring);
  160. }
  161. is_root = true;
  162. } else if (c == '@' && s == send) {
  163. // handle a single '@' as the root name.
  164. is_root = true;
  165. }
  166. if (is_root) {
  167. ndata.push_back(0);
  168. done = true;
  169. break;
  170. }
  171. // FALLTHROUGH
  172. case ft_start:
  173. ndata.push_back(0); // placeholder for the label length field
  174. count = 0;
  175. if (c == '\\') {
  176. state = ft_initialescape;
  177. break;
  178. }
  179. state = ft_ordinary;
  180. assert(ndata.size() < Name::MAX_WIRE);
  181. // FALLTHROUGH
  182. case ft_ordinary:
  183. if (c == '.') {
  184. if (count == 0) {
  185. isc_throw(EmptyLabel,
  186. "duplicate period in " << namestring);
  187. }
  188. ndata.at(offsets.back()) = count;
  189. offsets.push_back(ndata.size());
  190. if (s == send) {
  191. ndata.push_back(0);
  192. done = true;
  193. }
  194. state = ft_start;
  195. } else if (c == '\\') {
  196. state = ft_escape;
  197. } else {
  198. if (++count > MAX_LABELLEN) {
  199. isc_throw(TooLongLabel,
  200. "label is too long in " << namestring);
  201. }
  202. ndata.push_back(downcase ? maptolower[c] : c);
  203. }
  204. break;
  205. case ft_initialescape:
  206. if (c == '[') {
  207. // This looks like a bitstring label, which was deprecated.
  208. // Intentionally drop it.
  209. isc_throw(BadLabelType,
  210. "invalid label type in " << namestring);
  211. }
  212. state = ft_escape;
  213. // FALLTHROUGH
  214. case ft_escape:
  215. if (!isdigit(c & 0xff)) {
  216. if (++count > MAX_LABELLEN) {
  217. isc_throw(TooLongLabel,
  218. "label is too long in " << namestring);
  219. }
  220. ndata.push_back(downcase ? maptolower[c] : c);
  221. state = ft_ordinary;
  222. break;
  223. }
  224. digits = 0;
  225. value = 0;
  226. state = ft_escdecimal;
  227. // FALLTHROUGH
  228. case ft_escdecimal:
  229. if (!isdigit(c & 0xff)) {
  230. isc_throw(BadEscape,
  231. "mixture of escaped digit and non-digit in "
  232. << namestring);
  233. }
  234. value *= 10;
  235. value += digitvalue[c];
  236. digits++;
  237. if (digits == 3) {
  238. if (value > 255) {
  239. isc_throw(BadEscape,
  240. "escaped decimal is too large in "
  241. << namestring);
  242. }
  243. if (++count > MAX_LABELLEN) {
  244. isc_throw(TooLongLabel,
  245. "label is too long in " << namestring);
  246. }
  247. ndata.push_back(downcase ? maptolower[value] : value);
  248. state = ft_ordinary;
  249. }
  250. break;
  251. default:
  252. // impossible case
  253. assert(false);
  254. }
  255. }
  256. if (!done) { // no trailing '.' was found.
  257. if (ndata.size() == Name::MAX_WIRE) {
  258. isc_throw(TooLongName,
  259. "name is too long for termination in " << namestring);
  260. }
  261. assert(s == send);
  262. if (state != ft_ordinary && state != ft_at) {
  263. isc_throw(IncompleteName,
  264. "incomplete textual name in " <<
  265. (namestring.empty() ? "<empty>" : namestring));
  266. }
  267. if (state == ft_ordinary) {
  268. assert(count != 0);
  269. ndata.at(offsets.back()) = count;
  270. offsets.push_back(ndata.size());
  271. // add a trailing \0
  272. ndata.push_back('\0');
  273. }
  274. }
  275. labelcount_ = offsets.size();
  276. assert(labelcount_ > 0 && labelcount_ <= Name::MAX_LABELS);
  277. ndata_.assign(ndata.data(), ndata.size());
  278. length_ = ndata_.size();
  279. offsets_.assign(offsets.begin(), offsets.end());
  280. }
  281. namespace {
  282. ///
  283. /// Wire-format name parser states.
  284. ///
  285. typedef enum {
  286. fw_start = 0, // beginning of a label
  287. fw_ordinary, // inside an ordinary (non compressed) label
  288. fw_newcurrent // beginning of a compression pointer
  289. } fw_state;
  290. }
  291. Name::Name(InputBuffer& buffer, bool downcase) {
  292. NameOffsets offsets;
  293. offsets.reserve(Name::MAX_LABELS);
  294. /*
  295. * Initialize things to make the compiler happy; they're not required.
  296. */
  297. unsigned int n = 0;
  298. //
  299. // Set up.
  300. //
  301. bool done = false;
  302. unsigned int nused = 0;
  303. bool seen_pointer = false;
  304. fw_state state = fw_start;
  305. unsigned int cused = 0; // Bytes of compressed name data used
  306. unsigned int current = buffer.getPosition();
  307. unsigned int pos_begin = current;
  308. unsigned int biggest_pointer = current;
  309. // Make the compiler happy; this is not required.
  310. // XXX: bad style in that we initialize it with a dummy value and define
  311. // it far from where it's used. But alternatives seemed even worse.
  312. unsigned int new_current = 0;
  313. //
  314. // Note: The following code is not optimized for speed, but
  315. // rather for correctness. Speed will be addressed in the future.
  316. //
  317. while (current < buffer.getLength() && !done) {
  318. unsigned int c = buffer.readUint8();
  319. current++;
  320. if (!seen_pointer) {
  321. cused++;
  322. }
  323. switch (state) {
  324. case fw_start:
  325. if (c <= MAX_LABELLEN) {
  326. offsets.push_back(nused);
  327. if (nused + c + 1 > Name::MAX_WIRE) {
  328. isc_throw(DNSMessageFORMERR, "wire name is too long: "
  329. << nused + c + 1 << " bytes");
  330. }
  331. nused += c + 1;
  332. ndata_.push_back(c);
  333. if (c == 0) {
  334. done = true;
  335. }
  336. n = c;
  337. state = fw_ordinary;
  338. } else if ((c & COMPRESS_POINTER_MARK8) == COMPRESS_POINTER_MARK8) {
  339. //
  340. // Ordinary 14-bit pointer.
  341. //
  342. new_current = c & ~COMPRESS_POINTER_MARK8;
  343. n = 1;
  344. state = fw_newcurrent;
  345. } else {
  346. // this case includes local compression pointer, which hasn't
  347. // been standardized.
  348. isc_throw(DNSMessageFORMERR, "unknown label character: " << c);
  349. }
  350. break;
  351. case fw_ordinary:
  352. if (downcase) {
  353. c = maptolower[c];
  354. }
  355. ndata_.push_back(c);
  356. if (--n == 0) {
  357. state = fw_start;
  358. }
  359. break;
  360. case fw_newcurrent:
  361. new_current *= 256;
  362. new_current += c;
  363. if (--n != 0) {
  364. break;
  365. }
  366. if (new_current >= biggest_pointer) {
  367. isc_throw(DNSMessageFORMERR,
  368. "bad compression pointer (out of range): " <<
  369. new_current);
  370. }
  371. biggest_pointer = new_current;
  372. current = new_current;
  373. buffer.setPosition(current);
  374. seen_pointer = true;
  375. state = fw_start;
  376. break;
  377. default:
  378. assert(false);
  379. }
  380. }
  381. if (!done) {
  382. isc_throw(DNSMessageFORMERR, "incomplete wire-format name");
  383. }
  384. labelcount_ = offsets.size();
  385. length_ = nused;
  386. offsets_.assign(offsets.begin(), offsets.end());
  387. buffer.setPosition(pos_begin + cused);
  388. }
  389. void
  390. Name::toWire(OutputBuffer& buffer) const {
  391. buffer.writeData(ndata_.data(), ndata_.size());
  392. }
  393. void
  394. Name::toWire(AbstractMessageRenderer& renderer) const {
  395. renderer.writeName(*this);
  396. }
  397. std::string
  398. Name::toText(bool omit_final_dot) const {
  399. if (length_ == 1) {
  400. //
  401. // Special handling for the root label. We ignore omit_final_dot.
  402. //
  403. assert(labelcount_ == 1 && ndata_[0] == '\0');
  404. return (".");
  405. }
  406. NameString::const_iterator np = ndata_.begin();
  407. NameString::const_iterator np_end = ndata_.end();
  408. unsigned int labels = labelcount_; // use for integrity check
  409. // init with an impossible value to catch error cases in the end:
  410. unsigned int count = MAX_LABELLEN + 1;
  411. // result string: it will roughly have the same length as the wire format
  412. // name data. reserve that length to minimize reallocation.
  413. std::string result;
  414. result.reserve(length_);
  415. while (np != np_end) {
  416. labels--;
  417. count = *np++;
  418. if (count == 0) {
  419. if (!omit_final_dot) {
  420. result.push_back('.');
  421. }
  422. break;
  423. }
  424. if (count <= MAX_LABELLEN) {
  425. assert(np_end - np >= count);
  426. if (!result.empty()) {
  427. // just after a non-empty label. add a separating dot.
  428. result.push_back('.');
  429. }
  430. while (count-- > 0) {
  431. uint8_t c = *np++;
  432. switch (c) {
  433. case 0x22: // '"'
  434. case 0x28: // '('
  435. case 0x29: // ')'
  436. case 0x2E: // '.'
  437. case 0x3B: // ';'
  438. case 0x5C: // '\\'
  439. // Special modifiers in zone files.
  440. case 0x40: // '@'
  441. case 0x24: // '$'
  442. result.push_back('\\');
  443. result.push_back(c);
  444. break;
  445. default:
  446. if (c > 0x20 && c < 0x7f) {
  447. // append printable characters intact
  448. result.push_back(c);
  449. } else {
  450. // encode non-printable characters in the form of \DDD
  451. result.push_back(0x5c);
  452. result.push_back(0x30 + ((c / 100) % 10));
  453. result.push_back(0x30 + ((c / 10) % 10));
  454. result.push_back(0x30 + (c % 10));
  455. }
  456. }
  457. }
  458. } else {
  459. isc_throw(BadLabelType, "unknown label type in name data");
  460. }
  461. }
  462. assert(labels == 0);
  463. assert(count == 0); // a valid name must end with a 'dot'.
  464. return (result);
  465. }
  466. NameComparisonResult
  467. Name::compare(const Name& other) const {
  468. return (compare(other, 0, 0, labelcount_, other.labelcount_));
  469. }
  470. NameComparisonResult
  471. Name::compare(const Name& other,
  472. unsigned int first_label,
  473. unsigned int first_label_other,
  474. unsigned int last_label,
  475. unsigned int last_label_other,
  476. bool case_sensitive) const {
  477. // Determine the relative ordering under the DNSSEC order relation of
  478. // 'this' and 'other', and also determine the hierarchical relationship
  479. // of the names.
  480. if ((first_label > last_label) ||
  481. (first_label_other > last_label_other)) {
  482. isc_throw(BadValue, "Bad label index ranges were passed");
  483. }
  484. if ((first_label > labelcount_) ||
  485. (first_label_other > other.labelcount_)) {
  486. isc_throw(BadValue, "Bad first label indices were passed");
  487. }
  488. unsigned int nlabels = 0;
  489. int l1 = last_label - first_label;
  490. int l2 = last_label_other - first_label_other;
  491. int ldiff = (int)l1 - (int)l2;
  492. unsigned int l = (ldiff < 0) ? l1 : l2;
  493. while (l > 0) {
  494. --l;
  495. --l1;
  496. --l2;
  497. size_t pos1 = offsets_[l1 + first_label];
  498. size_t pos2 = other.offsets_[l2 + first_label_other];
  499. unsigned int count1 = ndata_[pos1++];
  500. unsigned int count2 = other.ndata_[pos2++];
  501. // We don't support any extended label types including now-obsolete
  502. // bitstring labels.
  503. assert(count1 <= MAX_LABELLEN && count2 <= MAX_LABELLEN);
  504. int cdiff = (int)count1 - (int)count2;
  505. unsigned int count = (cdiff < 0) ? count1 : count2;
  506. while (count > 0) {
  507. uint8_t label1 = ndata_[pos1];
  508. uint8_t label2 = other.ndata_[pos2];
  509. int chdiff;
  510. if (case_sensitive) {
  511. chdiff = (int)label1 - (int)label2;
  512. } else {
  513. chdiff = (int)maptolower[label1] - (int)maptolower[label2];
  514. }
  515. if (chdiff != 0) {
  516. if ((nlabels == 0) &&
  517. ((last_label < labelcount_) ||
  518. (last_label_other < other.labelcount_))) {
  519. return (NameComparisonResult(0, 0,
  520. NameComparisonResult::NONE));
  521. } else {
  522. return (NameComparisonResult(chdiff, nlabels,
  523. NameComparisonResult::COMMONANCESTOR));
  524. }
  525. }
  526. --count;
  527. ++pos1;
  528. ++pos2;
  529. }
  530. if (cdiff != 0) {
  531. if ((nlabels == 0) &&
  532. ((last_label < labelcount_) ||
  533. (last_label_other < other.labelcount_))) {
  534. return (NameComparisonResult(0, 0,
  535. NameComparisonResult::NONE));
  536. } else {
  537. return (NameComparisonResult(cdiff, nlabels,
  538. NameComparisonResult::COMMONANCESTOR));
  539. }
  540. }
  541. ++nlabels;
  542. }
  543. if (ldiff < 0) {
  544. return (NameComparisonResult(ldiff, nlabels,
  545. NameComparisonResult::SUPERDOMAIN));
  546. } else if (ldiff > 0) {
  547. return (NameComparisonResult(ldiff, nlabels,
  548. NameComparisonResult::SUBDOMAIN));
  549. }
  550. return (NameComparisonResult(ldiff, nlabels, NameComparisonResult::EQUAL));
  551. }
  552. bool
  553. Name::equals(const Name& other) const {
  554. if (length_ != other.length_ || labelcount_ != other.labelcount_) {
  555. return (false);
  556. }
  557. for (unsigned int l = labelcount_, pos = 0; l > 0; --l) {
  558. uint8_t count = ndata_[pos];
  559. if (count != other.ndata_[pos]) {
  560. return (false);
  561. }
  562. ++pos;
  563. while (count-- > 0) {
  564. uint8_t label1 = ndata_[pos];
  565. uint8_t label2 = other.ndata_[pos];
  566. if (maptolower[label1] != maptolower[label2]) {
  567. return (false);
  568. }
  569. ++pos;
  570. }
  571. }
  572. return (true);
  573. }
  574. bool
  575. Name::leq(const Name& other) const {
  576. return (compare(other).getOrder() <= 0);
  577. }
  578. bool
  579. Name::geq(const Name& other) const {
  580. return (compare(other).getOrder() >= 0);
  581. }
  582. bool
  583. Name::lthan(const Name& other) const {
  584. return (compare(other).getOrder() < 0);
  585. }
  586. bool
  587. Name::gthan(const Name& other) const {
  588. return (compare(other).getOrder() > 0);
  589. }
  590. bool
  591. Name::isWildcard() const {
  592. return (length_ >= 2 && ndata_[0] == 1 && ndata_[1] == '*');
  593. }
  594. Name
  595. Name::concatenate(const Name& suffix) const {
  596. assert(length_ > 0 && suffix.length_ > 0);
  597. assert(labelcount_ > 0 && suffix.labelcount_ > 0);
  598. unsigned int length = length_ + suffix.length_ - 1;
  599. if (length > Name::MAX_WIRE) {
  600. isc_throw(TooLongName, "names are too long to concatenate");
  601. }
  602. Name retname;
  603. retname.ndata_.reserve(length);
  604. retname.ndata_.assign(ndata_, 0, length_ - 1);
  605. retname.ndata_.insert(retname.ndata_.end(),
  606. suffix.ndata_.begin(), suffix.ndata_.end());
  607. assert(retname.ndata_.size() == length);
  608. retname.length_ = length;
  609. //
  610. // Setup the offsets vector. Copy the offsets of this (prefix) name,
  611. // excluding that for the trailing dot, and append the offsets of the
  612. // suffix name with the additional offset of the length of the prefix.
  613. //
  614. unsigned int labels = labelcount_ + suffix.labelcount_ - 1;
  615. assert(labels <= Name::MAX_LABELS);
  616. retname.offsets_.reserve(labels);
  617. retname.offsets_.assign(&offsets_[0], &offsets_[0] + labelcount_ - 1);
  618. transform(suffix.offsets_.begin(), suffix.offsets_.end(),
  619. back_inserter(retname.offsets_),
  620. bind2nd(plus<char>(), length_ - 1));
  621. assert(retname.offsets_.size() == labels);
  622. retname.labelcount_ = labels;
  623. return (retname);
  624. }
  625. Name
  626. Name::reverse() const {
  627. Name retname;
  628. //
  629. // Set up offsets: The size of the string and number of labels will
  630. // be the same in as in the original.
  631. //
  632. retname.offsets_.reserve(labelcount_);
  633. retname.ndata_.reserve(length_);
  634. // Copy the original name, label by label, from tail to head.
  635. NameOffsets::const_reverse_iterator rit0 = offsets_.rbegin();
  636. NameOffsets::const_reverse_iterator rit1 = rit0 + 1;
  637. NameString::const_iterator n0 = ndata_.begin();
  638. retname.offsets_.push_back(0);
  639. while (rit1 != offsets_.rend()) {
  640. retname.ndata_.append(n0 + *rit1, n0 + *rit0);
  641. retname.offsets_.push_back(retname.ndata_.size());
  642. ++rit0;
  643. ++rit1;
  644. }
  645. retname.ndata_.push_back(0);
  646. retname.labelcount_ = labelcount_;
  647. retname.length_ = length_;
  648. return (retname);
  649. }
  650. Name
  651. Name::split(const unsigned int first, const unsigned int n) const {
  652. if (n == 0 || n > labelcount_ || first > labelcount_ - n) {
  653. isc_throw(OutOfRange, "Name::split: invalid split range");
  654. }
  655. Name retname;
  656. // If the specified range doesn't include the trailing dot, we need one
  657. // more label for that.
  658. unsigned int newlabels = (first + n == labelcount_) ? n : n + 1;
  659. //
  660. // Set up offsets: copy the corresponding range of the original offsets
  661. // with subtracting an offset of the prefix length.
  662. //
  663. retname.offsets_.reserve(newlabels);
  664. transform(offsets_.begin() + first, offsets_.begin() + first + newlabels,
  665. back_inserter(retname.offsets_),
  666. bind2nd(plus<char>(), -offsets_[first]));
  667. //
  668. // Set up the new name. At this point the tail of the new offsets specifies
  669. // the position of the trailing dot, which should be equal to the length of
  670. // the extracted portion excluding the dot. First copy that part from the
  671. // original name, and append the trailing dot explicitly.
  672. //
  673. retname.ndata_.reserve(retname.offsets_.back() + 1);
  674. retname.ndata_.assign(ndata_, offsets_[first], retname.offsets_.back());
  675. retname.ndata_.push_back(0);
  676. retname.length_ = retname.ndata_.size();
  677. retname.labelcount_ = retname.offsets_.size();
  678. assert(retname.labelcount_ == newlabels);
  679. return (retname);
  680. }
  681. Name
  682. Name::split(const unsigned int level) const {
  683. if (level >= getLabelCount()) {
  684. isc_throw(OutOfRange, "invalid level for name split (" << level
  685. << ") for name " << *this);
  686. }
  687. return (split(level, getLabelCount() - level));
  688. }
  689. Name&
  690. Name::downcase() {
  691. unsigned int nlen = length_;
  692. unsigned int labels = labelcount_;
  693. unsigned int pos = 0;
  694. while (labels > 0 && nlen > 0) {
  695. --labels;
  696. --nlen;
  697. // we assume a valid name, and do abort() if the assumption fails
  698. // rather than throwing an exception.
  699. unsigned int count = ndata_.at(pos++);
  700. assert(count <= MAX_LABELLEN);
  701. assert(nlen >= count);
  702. while (count > 0) {
  703. ndata_.at(pos) =
  704. maptolower[ndata_.at(pos)];
  705. ++pos;
  706. --nlen;
  707. --count;
  708. }
  709. }
  710. return (*this);
  711. }
  712. std::ostream&
  713. operator<<(std::ostream& os, const Name& name) {
  714. os << name.toText();
  715. return (os);
  716. }
  717. }
  718. }