name.cc 25 KB

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