name.cc 25 KB

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