Kea 3.1.5
data.cc
Go to the documentation of this file.
1// Copyright (C) 2010-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/data.h>
10#include <util/bigints.h>
11
12#include <cstring>
13#include <cassert>
14#include <climits>
15#include <list>
16#include <map>
17#include <cstdio>
18#include <iostream>
19#include <iomanip>
20#include <set>
21#include <string>
22#include <sstream>
23#include <fstream>
24#include <cerrno>
25
26#include <boost/lexical_cast.hpp>
27
28#include <cmath>
29
30using namespace std;
31
33
34namespace {
35const char* const WHITESPACE = " \b\f\n\r\t";
36} // end anonymous namespace
37
38namespace isc {
39namespace data {
40
41constexpr unsigned Element::MAX_NESTING_LEVEL;
42
43std::string
45 std::ostringstream ss;
46 ss << file_ << ":" << line_ << ":" << pos_;
47 return (ss.str());
48}
49
50std::ostream&
51operator<<(std::ostream& out, const Element::Position& pos) {
52 out << pos.str();
53 return (out);
54}
55
56void
58 if (level <= 0) {
59 // Cycles are by definition not empty so no need to throw.
60 return;
61 }
62 if (type_ == list || type_ == map) {
63 size_t s(size());
64 for (size_t i = 0; i < s; ++i) {
65 // Get child.
66 ElementPtr child;
67 if (type_ == list) {
68 child = getNonConst(i);
69 } else if (type_ == map) {
70 std::string const key(get(i)->stringValue());
71 // The ElementPtr - ConstElementPtr disparity between
72 // ListElement and MapElement is forcing a const cast here.
73 // It's undefined behavior to modify it after const casting.
74 // The options are limited. I've tried templating, moving
75 // this function from a member function to free-standing and
76 // taking the Element template as argument. I've tried
77 // making it a virtual function with overridden
78 // implementations in ListElement and MapElement. Nothing
79 // works.
80 child = boost::const_pointer_cast<Element>(get(key));
81 }
82
83 // Makes no sense to continue for non-container children.
84 if (child->getType() != list && child->getType() != map) {
85 continue;
86 }
87
88 // Recurse if not empty.
89 if (!child->empty()){
90 child->removeEmptyContainersRecursively(level - 1);
91 }
92
93 // When returning from recursion, remove if empty.
94 if (child->empty()) {
95 remove(i);
96 --i;
97 --s;
98 }
99 }
100 }
101}
102
103std::string
105 std::stringstream ss;
106 toJSON(ss);
107 return (ss.str());
108}
109
110std::string
112 std::stringstream ss;
113 toJSON(ss);
114 return (ss.str());
115}
116
117void
118Element::toWire(std::ostream& ss) const {
119 toJSON(ss);
120}
121
122bool
123Element::getValue(int64_t&) const {
124 return (false);
125}
126
127bool
128Element::getValue(double&) const {
129 return (false);
130}
131
132bool
133Element::getValue(bool&) const {
134 return (false);
135}
136
137bool
138Element::getValue(std::string&) const {
139 return (false);
140}
141
142bool
143Element::getValue(std::vector<ElementPtr>&) const {
144 return (false);
145}
146
147bool
148Element::getValue(std::map<std::string, ConstElementPtr>&) const {
149 return (false);
150}
151
152bool
153Element::setValue(const long long int) {
154 return (false);
155}
156
157bool
159 return (false);
160}
161
162bool
163Element::setValue(const double) {
164 return (false);
165}
166
167bool
168Element::setValue(const bool) {
169 return (false);
170}
171
172bool
173Element::setValue(const std::string&) {
174 return (false);
175}
176
177bool
178Element::setValue(const std::vector<ElementPtr>&) {
179 return (false);
180}
181
182bool
183Element::setValue(const std::map<std::string, ConstElementPtr>&) {
184 return (false);
185}
186
188Element::get(const int) const {
189 throwTypeError("get(int) called on a non-container Element");
190}
191
193Element::getNonConst(const int) const {
194 throwTypeError("get(int) called on a non-container Element");
195}
196
197void
198Element::set(const size_t, ElementPtr) {
199 throwTypeError("set(int, element) called on a non-list Element");
200}
201
202void
204 throwTypeError("add() called on a non-list Element");
205}
206
207void
208Element::remove(const int) {
209 throwTypeError("remove(int) called on a non-container Element");
210}
211
212size_t
214 throwTypeError("size() called on a non-list Element");
215}
216
217bool
219 throwTypeError("empty() called on a non-container Element");
220}
221
223Element::get(const std::string&) const {
224 throwTypeError("get(string) called on a non-map Element");
225}
226
227void
228Element::set(const std::string&, ConstElementPtr) {
229 throwTypeError("set(name, element) called on a non-map Element");
230}
231
232void
233Element::remove(const std::string&) {
234 throwTypeError("remove(string) called on a non-map Element");
235}
236
237bool
238Element::contains(const std::string&) const {
239 throwTypeError("contains(string) called on a non-map Element");
240}
241
243Element::find(const std::string&) const {
244 throwTypeError("find(string) called on a non-map Element");
245}
246
247bool
248Element::find(const std::string&, ConstElementPtr&) const {
249 return (false);
250}
251
252namespace {
253inline void
254throwJSONError(const std::string& error, const std::string& file, int line,
255 int pos) {
256 std::stringstream ss;
257 ss << error << " in " + file + ":" << line << ":" << pos;
258 isc_throw(JSONError, ss.str());
259}
260} // end anonymous namespace
261
262std::ostream&
263operator<<(std::ostream& out, const Element& e) {
264 return (out << e.str());
265}
266
267bool
268operator==(const Element& a, const Element& b) {
269 return (a.equals(b));
270}
271
272bool operator!=(const Element& a, const Element& b) {
273 return (!a.equals(b));
274}
275
276bool
277operator<(Element const& a, Element const& b) {
278 if (a.getType() != b.getType()) {
279 isc_throw(BadValue, "cannot compare Elements of different types");
280 }
281 switch (a.getType()) {
282 case Element::integer:
283 return a.intValue() < b.intValue();
284 case Element::real:
285 return a.doubleValue() < b.doubleValue();
286 case Element::boolean:
287 return b.boolValue() || !a.boolValue();
288 case Element::string:
289 return std::strcmp(a.stringValue().c_str(), b.stringValue().c_str()) < 0;
290 default:
291 isc_throw(BadValue, "cannot compare Elements of type " << to_string(a.getType()));
292 }
293}
294
295//
296// factory functions
297//
300 return (ElementPtr(new NullElement(pos)));
301}
302
304Element::create(const long long int i, const Position& pos) {
305 return (ElementPtr(new IntElement(static_cast<int64_t>(i), pos)));
306}
307
310 return (ElementPtr(new BigIntElement(i, pos)));
311}
312
314Element::create(const int i, const Position& pos) {
315 return (create(static_cast<long long int>(i), pos));
316}
317
319Element::create(const long int i, const Position& pos) {
320 return (create(static_cast<long long int>(i), pos));
321}
322
324Element::create(const uint32_t i, const Position& pos) {
325 return (create(static_cast<long long int>(i), pos));
326}
327
329Element::create(const double d, const Position& pos) {
330 return (ElementPtr(new DoubleElement(d, pos)));
332
334Element::create(const bool b, const Position& pos) {
335 return (ElementPtr(new BoolElement(b, pos)));
336}
339Element::create(const std::string& s, const Position& pos) {
340 return (ElementPtr(new StringElement(s, pos)));
341}
342
344Element::create(const char *s, const Position& pos) {
345 return (create(std::string(s), pos));
346}
347
350 return (ElementPtr(new ListElement(pos)));
351}
352
355 return (ElementPtr(new MapElement(pos)));
356}
357
358
359//
360// helper functions for fromJSON factory
361//
362namespace {
363bool
364charIn(const int c, const char* chars) {
365 const size_t chars_len = std::strlen(chars);
366 for (size_t i = 0; i < chars_len; ++i) {
367 if (chars[i] == c) {
368 return (true);
369 }
370 }
371 return (false);
373
374void
375skipChars(std::istream& in, const char* chars, int& line, int& pos) {
376 int c = in.peek();
377 while (charIn(c, chars) && c != EOF) {
378 if (c == '\n') {
379 ++line;
380 pos = 1;
381 } else {
382 ++pos;
383 }
384 in.ignore();
385 c = in.peek();
386 }
387}
388
389// skip on the input stream to one of the characters in chars
390// if another character is found this function throws JSONError
391// unless that character is specified in the optional may_skip
392//
393// It returns the found character (as an int value).
394int
395skipTo(std::istream& in, const std::string& file, int& line, int& pos,
396 const char* chars, const char* may_skip="") {
397 int c = in.get();
398 ++pos;
399 while (c != EOF) {
400 if (c == '\n') {
401 pos = 1;
402 ++line;
403 }
404 if (charIn(c, may_skip)) {
405 c = in.get();
406 ++pos;
407 } else if (charIn(c, chars)) {
408 while (charIn(in.peek(), may_skip)) {
409 if (in.peek() == '\n') {
410 pos = 1;
411 ++line;
412 } else {
413 ++pos;
414 }
415 in.ignore();
416 }
417 return (c);
418 } else {
419 throwJSONError(std::string("'") + std::string(1, c) + "' read, one of \"" + chars + "\" expected", file, line, pos);
420 }
421 }
422 throwJSONError(std::string("EOF read, one of \"") + chars + "\" expected", file, line, pos);
423 return (c); // shouldn't reach here, but some compilers require it
424}
425
426// TODO: Should we check for all other official escapes here (and
427// error on the rest)?
428std::string
429strFromStringstream(std::istream& in, const std::string& file,
430 const int line, int& pos) {
431 std::stringstream ss;
432 int c = in.get();
433 ++pos;
434 if (c == '"') {
435 c = in.get();
436 ++pos;
437 } else {
438 throwJSONError("String expected", file, line, pos);
439 }
440
441 while (c != EOF && c != '"') {
442 if (c == '\\') {
443 // see the spec for allowed escape characters
444 int d;
445 switch (in.peek()) {
446 case '"':
447 c = '"';
448 break;
449 case '/':
450 c = '/';
451 break;
452 case '\\':
453 c = '\\';
454 break;
455 case 'b':
456 c = '\b';
457 break;
458 case 'f':
459 c = '\f';
460 break;
461 case 'n':
462 c = '\n';
463 break;
464 case 'r':
465 c = '\r';
466 break;
467 case 't':
468 c = '\t';
469 break;
470 case 'u':
471 // skip first 0
472 in.ignore();
473 ++pos;
474 c = in.peek();
475 if (c != '0') {
476 throwJSONError("Unsupported unicode escape", file, line, pos);
477 }
478 // skip second 0
479 in.ignore();
480 ++pos;
481 c = in.peek();
482 if (c != '0') {
483 throwJSONError("Unsupported unicode escape", file, line, pos - 2);
485 // get first digit
486 in.ignore();
487 ++pos;
488 d = in.peek();
489 if ((d >= '0') && (d <= '9')) {
490 c = (d - '0') << 4;
491 } else if ((d >= 'A') && (d <= 'F')) {
492 c = (d - 'A' + 10) << 4;
493 } else if ((d >= 'a') && (d <= 'f')) {
494 c = (d - 'a' + 10) << 4;
495 } else {
496 throwJSONError("Not hexadecimal in unicode escape", file, line, pos - 3);
497 }
498 // get second digit
499 in.ignore();
500 ++pos;
501 d = in.peek();
502 if ((d >= '0') && (d <= '9')) {
503 c |= d - '0';
504 } else if ((d >= 'A') && (d <= 'F')) {
505 c |= d - 'A' + 10;
506 } else if ((d >= 'a') && (d <= 'f')) {
507 c |= d - 'a' + 10;
508 } else {
509 throwJSONError("Not hexadecimal in unicode escape", file, line, pos - 4);
510 }
511 break;
512 default:
513 throwJSONError("Bad escape", file, line, pos);
514 }
515 // drop the escaped char
516 in.ignore();
517 ++pos;
518 }
519 ss.put(c);
520 c = in.get();
521 ++pos;
522 }
523 if (c == EOF) {
524 throwJSONError("Unterminated string", file, line, pos);
525 }
526 return (ss.str());
527}
528
529std::string
530wordFromStringstream(std::istream& in, int& pos) {
531 std::stringstream ss;
532 while (isalpha(in.peek())) {
533 ss << (char) in.get();
534 }
535 pos += ss.str().size();
536 return (ss.str());
537}
538
539std::string
540numberFromStringstream(std::istream& in, int& pos) {
541 std::stringstream ss;
542 while (isdigit(in.peek()) || in.peek() == '+' || in.peek() == '-' ||
543 in.peek() == '.' || in.peek() == 'e' || in.peek() == 'E') {
544 ss << (char) in.get();
545 }
546 pos += ss.str().size();
547 return (ss.str());
548}
549
550// Should we change from IntElement and DoubleElement to NumberElement
551// that can also hold an e value? (and have specific getters if the
552// value is larger than an int can handle)
553//
554// At the moment of writing, the only way that the code flow can reach the
555// int128_t cast, and not throw, is by retrieving one of the few bigint
556// statistics through kea-ctrl-agent. If kea-ctrl-agent ever gets removed, and
557// its HTTP listener embedded in Kea, then the cast to int128_t can be removed as
558// well, as there is no deserialization of bigints required, although the only
559// benefit would be better performance for error cases, so it's arguable.
561fromStringstreamNumber(std::istream& in, const std::string& file,
562 const int line, int& pos) {
563 // Remember position where the value starts. It will be set in the
564 // Position structure of the Element to be created.
565 const uint32_t start_pos = pos;
566 // This will move the pos to the end of the value.
567 const std::string number = numberFromStringstream(in, pos);
568
569 // Is it a double?
570 if (number.find_first_of(".eE") < number.size()) {
571 try {
572 return (Element::create(boost::lexical_cast<double>(number),
573 Element::Position(file, line, start_pos)));
574 } catch (const boost::bad_lexical_cast& exception) {
575 throwJSONError("Number overflow while trying to cast '" + number +
576 "' to double: " + exception.what(),
577 file, line, start_pos);
578 }
579 }
580
581 // Is it an integer?
582 try {
583 return (Element::create(boost::lexical_cast<int64_t>(number),
584 Element::Position(file, line, start_pos)));
585 } catch (const boost::bad_lexical_cast& exception64) {
586 // Is it a big integer?
587 try {
588 return (Element::create(int128_t(number),
589 Element::Position(file, line, start_pos)));
590 } catch (overflow_error const& exception128) {
591 throwJSONError("Number overflow while trying to cast '" + number +
592 "' to int64 and subsequently to int128: " +
593 exception64.what() + ", " + exception128.what(),
594 file, line, start_pos);
595 }
596 }
597 return (ElementPtr());
598}
599
601fromStringstreamBool(std::istream& in, const std::string& file,
602 const int line, int& pos) {
603 // Remember position where the value starts. It will be set in the
604 // Position structure of the Element to be created.
605 const uint32_t start_pos = pos;
606 // This will move the pos to the end of the value.
607 const std::string word = wordFromStringstream(in, pos);
608
609 if (word == "true") {
610 return (Element::create(true, Element::Position(file, line,
611 start_pos)));
612 } else if (word == "false") {
613 return (Element::create(false, Element::Position(file, line,
614 start_pos)));
615 } else {
616 throwJSONError(std::string("Bad boolean value: ") + word, file,
617 line, start_pos);
618 }
619 return (ElementPtr());
620}
621
623fromStringstreamNull(std::istream& in, const std::string& file,
624 const int line, int& pos) {
625 // Remember position where the value starts. It will be set in the
626 // Position structure of the Element to be created.
627 const uint32_t start_pos = pos;
628 // This will move the pos to the end of the value.
629 const std::string word = wordFromStringstream(in, pos);
630 if (word == "null") {
631 return (Element::create(Element::Position(file, line, start_pos)));
632 } else {
633 throwJSONError(std::string("Bad null value: ") + word, file,
634 line, start_pos);
635 return (ElementPtr());
636 }
637}
638
640fromStringstreamString(std::istream& in, const std::string& file, int& line,
641 int& pos) {
642 // Remember position where the value starts. It will be set in the
643 // Position structure of the Element to be created.
644 const uint32_t start_pos = pos;
645 // This will move the pos to the end of the value.
646 const std::string string_value = strFromStringstream(in, file, line, pos);
647 return (Element::create(string_value, Element::Position(file, line,
648 start_pos)));
649}
650
652fromStringstreamList(std::istream& in, const std::string& file, int& line,
653 int& pos, unsigned level) {
654 if (level == 0) {
655 isc_throw(JSONError, "fromJSON elements nested too deeply");
656 }
657 int c = 0;
658 ElementPtr list = Element::createList(Element::Position(file, line, pos));
659 ElementPtr cur_list_element;
660
661 skipChars(in, WHITESPACE, line, pos);
662 while (c != EOF && c != ']') {
663 if (in.peek() != ']') {
664 cur_list_element =
665 Element::fromJSON(in, file, line, pos, level - 1);
666 list->add(cur_list_element);
667 c = skipTo(in, file, line, pos, ",]", WHITESPACE);
668 } else {
669 c = in.get();
670 ++pos;
671 }
672 }
673 return (list);
674}
675
677fromStringstreamMap(std::istream& in, const std::string& file, int& line,
678 int& pos, unsigned level) {
679 if (level == 0) {
680 isc_throw(JSONError, "fromJSON elements nested too deeply");
681 }
682 ElementPtr map = Element::createMap(Element::Position(file, line, pos));
683 skipChars(in, WHITESPACE, line, pos);
684 int c = in.peek();
685 if (c == EOF) {
686 throwJSONError(std::string("Unterminated map, <string> or } expected"), file, line, pos);
687 } else if (c == '}') {
688 // empty map, skip closing curly
689 in.ignore();
690 } else {
691 while (c != EOF && c != '}') {
692 std::string key = strFromStringstream(in, file, line, pos);
693
694 skipTo(in, file, line, pos, ":", WHITESPACE);
695 // skip the :
696
697 ConstElementPtr value =
698 Element::fromJSON(in, file, line, pos, level - 1);
699 map->set(key, value);
700
701 c = skipTo(in, file, line, pos, ",}", WHITESPACE);
702 }
703 }
704 return (map);
705}
706} // end anonymous namespace
707
708std::string
710 switch (type) {
711 case Element::integer:
712 return (std::string("integer"));
713 case Element::bigint:
714 return (std::string("bigint"));
715 case Element::real:
716 return (std::string("real"));
717 case Element::boolean:
718 return (std::string("boolean"));
719 case Element::string:
720 return (std::string("string"));
721 case Element::list:
722 return (std::string("list"));
723 case Element::map:
724 return (std::string("map"));
725 case Element::null:
726 return (std::string("null"));
727 case Element::any:
728 return (std::string("any"));
729 default:
730 return (std::string("unknown"));
731 }
732}
733
735Element::nameToType(const std::string& type_name) {
736 if (type_name == "integer") {
737 return (Element::integer);
738 } else if (type_name == "bigint") {
739 return (Element::bigint);
740 } else if (type_name == "real") {
741 return (Element::real);
742 } else if (type_name == "boolean") {
743 return (Element::boolean);
744 } else if (type_name == "string") {
745 return (Element::string);
746 } else if (type_name == "list") {
747 return (Element::list);
748 } else if (type_name == "map") {
749 return (Element::map);
750 } else if (type_name == "named_set") {
751 return (Element::map);
752 } else if (type_name == "null") {
753 return (Element::null);
754 } else if (type_name == "any") {
755 return (Element::any);
756 } else {
757 isc_throw(TypeError, type_name + " is not a valid type name");
758 }
759}
760
762Element::fromJSON(std::istream& in, bool preproc) {
763
764 int line = 1, pos = 1;
765 stringstream filtered;
766 if (preproc) {
767 preprocess(in, filtered);
768 }
769
770 ElementPtr value = fromJSON(preproc ? filtered : in, "<istream>", line, pos);
771
772 return (value);
773}
774
776Element::fromJSON(std::istream& in, const std::string& file_name, bool preproc) {
777 int line = 1, pos = 1;
778 stringstream filtered;
779 if (preproc) {
780 preprocess(in, filtered);
781 }
782 return (fromJSON(preproc ? filtered : in, file_name, line, pos));
783}
784
786Element::fromJSON(std::istream& in, const std::string& file, int& line,
787 int& pos, unsigned level) {
788 if (level == 0) {
789 isc_throw(JSONError, "fromJSON elements nested too deeply");
790 }
791 int c = 0;
792 ElementPtr element;
793 bool el_read = false;
794 skipChars(in, WHITESPACE, line, pos);
795 while (c != EOF && !el_read) {
796 c = in.get();
797 pos++;
798 switch(c) {
799 case '1':
800 case '2':
801 case '3':
802 case '4':
803 case '5':
804 case '6':
805 case '7':
806 case '8':
807 case '9':
808 case '0':
809 case '-':
810 case '+':
811 case '.':
812 in.putback(c);
813 --pos;
814 element = fromStringstreamNumber(in, file, line, pos);
815 el_read = true;
816 break;
817 case 't':
818 case 'f':
819 in.putback(c);
820 --pos;
821 element = fromStringstreamBool(in, file, line, pos);
822 el_read = true;
823 break;
824 case 'n':
825 in.putback(c);
826 --pos;
827 element = fromStringstreamNull(in, file, line, pos);
828 el_read = true;
829 break;
830 case '"':
831 in.putback('"');
832 --pos;
833 element = fromStringstreamString(in, file, line, pos);
834 el_read = true;
835 break;
836 case '[':
837 element = fromStringstreamList(in, file, line, pos, level);
838 el_read = true;
839 break;
840 case '{':
841 element = fromStringstreamMap(in, file, line, pos, level);
842 el_read = true;
843 break;
844 case EOF:
845 break;
846 default:
847 throwJSONError(std::string("error: unexpected character ") + std::string(1, c), file, line, pos);
848 break;
849 }
850 }
851 if (el_read) {
852 return (element);
853 } else {
854 isc_throw(JSONError, "nothing read");
855 }
856}
857
859Element::fromJSON(const std::string& in, bool preproc) {
860 std::stringstream ss;
861 ss << in;
862
863 int line = 1, pos = 1;
864 stringstream filtered;
865 if (preproc) {
866 preprocess(ss, filtered);
867 }
868 ElementPtr result(fromJSON(preproc ? filtered : ss, "<string>", line, pos));
869 skipChars(ss, WHITESPACE, line, pos);
870 // ss must now be at end
871 if (ss.peek() != EOF) {
872 throwJSONError("Extra data", "<string>", line, pos);
873 }
874 return result;
875}
876
878Element::fromJSONFile(const std::string& file_name, bool preproc) {
879 // zero out the errno to be safe
880 errno = 0;
881
882 std::ifstream infile(file_name.c_str(), std::ios::in | std::ios::binary);
883 if (!infile.is_open()) {
884 const char* error = strerror(errno);
885 isc_throw(InvalidOperation, "failed to read file '" << file_name
886 << "': " << error);
887 }
888
889 return (fromJSON(infile, file_name, preproc));
890}
891
892// to JSON format
893
894void
895IntElement::toJSON(std::ostream& ss, unsigned) const {
896 ss << intValue();
897}
898
899void
900BigIntElement::toJSON(std::ostream& ss, unsigned) const {
901 ss << bigIntValue();
902}
903
904void
905DoubleElement::toJSON(std::ostream& ss, unsigned) const {
906 // The default output for doubles nicely drops off trailing
907 // zeros, however this produces strings without decimal points
908 // for whole number values. When reparsed this will create
909 // IntElements not DoubleElements. Rather than used a fixed
910 // precision, we'll just tack on an ".0" when the decimal point
911 // is missing.
912 ostringstream val_ss;
913 val_ss << doubleValue();
914 ss << val_ss.str();
915 if (val_ss.str().find_first_of('.') == string::npos) {
916 ss << ".0";
917 }
918}
919
920void
921BoolElement::toJSON(std::ostream& ss, unsigned) const {
922 if (boolValue()) {
923 ss << "true";
924 } else {
925 ss << "false";
926 }
927}
928
929void
930NullElement::toJSON(std::ostream& ss, unsigned) const {
931 ss << "null";
932}
933
934void
935StringElement::toJSON(std::ostream& ss, unsigned) const {
936 ss << "\"";
937 const std::string& str = stringValue();
938 for (size_t i = 0; i < str.size(); ++i) {
939 const signed char c = str[i];
940 // Escape characters as defined in JSON spec
941 // Note that we do not escape forward slash; this
942 // is allowed, but not mandatory.
943 switch (c) {
944 case '"':
945 ss << '\\' << c;
946 break;
947 case '\\':
948 ss << '\\' << c;
949 break;
950 case '\b':
951 ss << '\\' << 'b';
952 break;
953 case '\f':
954 ss << '\\' << 'f';
955 break;
956 case '\n':
957 ss << '\\' << 'n';
958 break;
959 case '\r':
960 ss << '\\' << 'r';
961 break;
962 case '\t':
963 ss << '\\' << 't';
964 break;
965 default:
966 if (c < 0x20 || c == 0x7f) {
967 std::ostringstream esc;
968 esc << "\\u"
969 << hex
970 << setw(4)
971 << setfill('0')
972 << (static_cast<unsigned>(c) & 0xff);
973 ss << esc.str();
974 } else {
975 ss << c;
976 }
977 }
978 }
979 ss << "\"";
980}
981
982void
983ListElement::toJSON(std::ostream& ss, unsigned level) const {
984 if (level == 0) {
985 isc_throw(BadValue, "toJSON got infinite recursion: "
986 "arguments include cycles");
987 }
988 ss << "[ ";
989
990 const std::vector<ElementPtr>& v = listValue();
991 bool first = true;
992 for (auto const& it : v) {
993 if (!first) {
994 ss << ", ";
995 } else {
996 first = false;
997 }
998 it->toJSON(ss, level - 1);
999 }
1000 ss << " ]";
1001}
1002
1003void
1004MapElement::toJSON(std::ostream& ss, unsigned level) const {
1005 if (level == 0) {
1006 isc_throw(BadValue, "toJSON got infinite recursion: "
1007 "arguments include cycles");
1008 }
1009 ss << "{ ";
1010
1011 bool first = true;
1012 for (auto const& it : m) {
1013 if (!first) {
1014 ss << ", ";
1015 } else {
1016 first = false;
1017 }
1018 ss << "\"" << it.first << "\": ";
1019 if (it.second) {
1020 it.second->toJSON(ss, level - 1);
1021 } else {
1022 ss << "None";
1023 }
1024 }
1025 ss << " }";
1026}
1027
1028// throws when one of the types in the path (except the one
1029// we're looking for) is not a MapElement
1030// returns 0 if it could simply not be found
1031// should that also be an exception?
1033MapElement::find(const std::string& id) const {
1034 const size_t sep = id.find('/');
1035 if (sep == std::string::npos) {
1036 return (get(id));
1037 } else {
1038 ConstElementPtr ce = get(id.substr(0, sep));
1039 if (ce) {
1040 // ignore trailing slash
1041 if (sep + 1 != id.size()) {
1042 return (ce->find(id.substr(sep + 1)));
1043 } else {
1044 return (ce);
1045 }
1046 } else {
1047 return (ElementPtr());
1048 }
1049 }
1050}
1051
1053Element::fromWire(const std::string& s) {
1054 std::stringstream ss;
1055 ss << s;
1056 int line = 0, pos = 0;
1057 return (fromJSON(ss, "<wire>", line, pos));
1058}
1059
1061Element::fromWire(std::stringstream& in, int) {
1062 //
1063 // Check protocol version
1064 //
1065 //for (int i = 0 ; i < 4 ; ++i) {
1066 // const unsigned char version_byte = get_byte(in);
1067 // if (PROTOCOL_VERSION[i] != version_byte) {
1068 // throw DecodeError("Protocol version incorrect");
1069 // }
1070 //}
1071 //length -= 4;
1072 int line = 0, pos = 0;
1073 return (fromJSON(in, "<wire>", line, pos));
1074}
1075
1076void
1077MapElement::set(const std::string& key, ConstElementPtr value) {
1078 m[key] = value;
1079}
1080
1081bool
1082MapElement::find(const std::string& id, ConstElementPtr& t) const {
1083 try {
1084 ConstElementPtr p = find(id);
1085 if (p) {
1086 t = p;
1087 return (true);
1088 }
1089 } catch (const TypeError&) {
1090 // ignore
1091 }
1092 return (false);
1093}
1094
1095bool
1096IntElement::equals(const Element& other, unsigned) const {
1097 // Let's not be very picky with constraining the integer types to be the
1098 // same. Equality is sometimes checked from high-up in the Element hierarchy.
1099 // That is a context which, most of the time, does not have information on
1100 // the type of integers stored on Elements lower in the hierarchy. So it
1101 // would be difficult to differentiate between the integer types.
1102 return (other.getType() == Element::integer && i == other.intValue()) ||
1103 (other.getType() == Element::bigint && i == other.bigIntValue());
1104}
1105
1106bool
1107BigIntElement::equals(const Element& other, unsigned) const {
1108 // Let's not be very picky with constraining the integer types to be the
1109 // same. Equality is sometimes checked from high-up in the Element hierarchy.
1110 // That is a context which, most of the time, does not have information on
1111 // the type of integers stored on Elements lower in the hierarchy. So it
1112 // would be difficult to differentiate between the integer types.
1113 return (other.getType() == Element::bigint && i_ == other.bigIntValue()) ||
1114 (other.getType() == Element::integer && i_ == other.intValue());
1115}
1116
1117bool
1118DoubleElement::equals(const Element& other, unsigned) const {
1119 return (other.getType() == Element::real) &&
1120 (fabs(d - other.doubleValue()) < 1e-14);
1121}
1122
1123bool
1124BoolElement::equals(const Element& other, unsigned) const {
1125 return (other.getType() == Element::boolean) &&
1126 (b == other.boolValue());
1127}
1128
1129bool
1130NullElement::equals(const Element& other, unsigned) const {
1131 return (other.getType() == Element::null);
1132}
1133
1134bool
1135StringElement::equals(const Element& other, unsigned) const {
1136 return (other.getType() == Element::string) &&
1137 (s == other.stringValue());
1138}
1139
1140bool
1141ListElement::equals(const Element& other, unsigned level) const {
1142 if (level == 0) {
1143 isc_throw(BadValue, "equals got infinite recursion: "
1144 "arguments include cycles");
1145 }
1146 if (other.getType() == Element::list) {
1147 const size_t s = size();
1148 if (s != other.size()) {
1149 return (false);
1150 }
1151 for (size_t i = 0; i < s; ++i) {
1152 if (!get(i)->equals(*other.get(i), level - 1)) {
1153 return (false);
1154 }
1155 }
1156 return (true);
1157 } else {
1158 return (false);
1159 }
1160}
1161
1162void
1163ListElement::sort(std::string const& index /* = std::string() */) {
1164 if (l.empty()) {
1165 return;
1166 }
1167
1168 int const t(l.at(0)->getType());
1169 std::function<bool(ElementPtr, ElementPtr)> comparator;
1170 if (t == map) {
1171 if (index.empty()) {
1172 isc_throw(BadValue, "index required when sorting maps");
1173 }
1174 comparator = [&](ElementPtr const& a, ElementPtr const& b) {
1175 ConstElementPtr const& ai(a->get(index));
1176 ConstElementPtr const& bi(b->get(index));
1177 if (ai && bi) {
1178 return *ai < *bi;
1179 }
1180 return true;
1181 };
1182 } else if (t == list) {
1183 // Nested lists. Not supported.
1184 return;
1185 } else {
1186 // Assume scalars.
1187 if (!index.empty()) {
1188 isc_throw(BadValue, "index given when sorting scalars?");
1189 }
1190 comparator = [&](ElementPtr const& a, ElementPtr const& b) {
1191 return *a < *b;
1192 };
1193 }
1194
1195 std::sort(l.begin(), l.end(), comparator);
1196}
1197
1198bool
1199MapElement::equals(const Element& other, unsigned level) const {
1200 if (level == 0) {
1201 isc_throw(BadValue, "equals got infinite recursion: "
1202 "arguments include cycles");
1203 }
1204 if (other.getType() == Element::map) {
1205 if (size() != other.size()) {
1206 return (false);
1207 }
1208 for (auto const& kv : mapValue()) {
1209 auto key = kv.first;
1210 if (other.contains(key)) {
1211 if (!get(key)->equals(*other.get(key), level - 1)) {
1212 return (false);
1213 }
1214 } else {
1215 return (false);
1216 }
1217 }
1218 return (true);
1219 } else {
1220 return (false);
1221 }
1222}
1223
1224bool
1226 return (!p);
1227}
1228
1229void
1231 if (!b) {
1232 return;
1233 }
1234 if (a->getType() != Element::map || b->getType() != Element::map) {
1235 isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
1236 }
1237
1238 // As maps do not allow entries with multiple keys, we can either iterate
1239 // over a checking for identical entries in b or vice-versa. As elements
1240 // are removed from a if a match is found, we choose to iterate over b to
1241 // avoid problems with element removal affecting the iterator.
1242 for (auto const& kv : b->mapValue()) {
1243 auto key = kv.first;
1244 if (a->contains(key)) {
1245 if (a->get(key)->equals(*b->get(key))) {
1246 a->remove(key);
1247 }
1248 }
1249 }
1250}
1251
1254 ElementPtr result = Element::createMap();
1255
1256 if (!b) {
1257 return (result);
1258 }
1259
1260 if (a->getType() != Element::map || b->getType() != Element::map) {
1261 isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
1262 }
1263
1264 for (auto const& kv : a->mapValue()) {
1265 auto key = kv.first;
1266 if (!b->contains(key) ||
1267 !a->get(key)->equals(*b->get(key))) {
1268 result->set(key, kv.second);
1269 }
1270 }
1271
1272 return (result);
1273}
1274
1275void
1277 if (element->getType() != Element::map ||
1278 other->getType() != Element::map) {
1279 isc_throw(TypeError, "merge arguments not MapElements");
1280 }
1281
1282 for (auto const& kv : other->mapValue()) {
1283 auto key = kv.first;
1284 auto value = kv.second;
1285 if (value && value->getType() != Element::null) {
1286 element->set(key, value);
1287 } else if (element->contains(key)) {
1288 element->remove(key);
1289 }
1290 }
1291}
1292
1293void
1295 HierarchyDescriptor& hierarchy, std::string key, size_t idx,
1296 unsigned level) {
1297 if (level == 0) {
1298 isc_throw(BadValue, "mergeDiffAdd got infinite recursion: "
1299 "arguments include cycles");
1300 }
1301 if (element->getType() != other->getType()) {
1302 isc_throw(TypeError, "mergeDiffAdd arguments not same type");
1303 }
1304
1305 if (element->getType() == Element::list) {
1306 // Store new elements in a separate container so we don't overwrite
1307 // options as we add them (if there are duplicates).
1308 ElementPtr new_elements = Element::createList();
1309 for (auto const& right : other->listValue()) {
1310 // Check if we have any description of the key in the configuration
1311 // hierarchy.
1312 auto f = hierarchy[idx].find(key);
1313 if (f != hierarchy[idx].end()) {
1314 bool found = false;
1315 ElementPtr mutable_right = boost::const_pointer_cast<Element>(right);
1316 for (auto const& left : element->listValue()) {
1317 ElementPtr mutable_left = boost::const_pointer_cast<Element>(left);
1318 // Check if the elements refer to the same configuration
1319 // entity.
1320 if (f->second.match_(mutable_left, mutable_right)) {
1321 found = true;
1322 mergeDiffAdd(mutable_left, mutable_right, hierarchy,
1323 key, idx, level - 1);
1324 }
1325 }
1326 if (!found) {
1327 new_elements->add(right);
1328 }
1329 } else {
1330 new_elements->add(right);
1331 }
1332 }
1333 // Finally add the new elements.
1334 for (auto const& right : new_elements->listValue()) {
1335 element->add(right);
1336 }
1337 return;
1338 }
1339
1340 if (element->getType() == Element::map) {
1341 for (auto const& kv : other->mapValue()) {
1342 auto current_key = kv.first;
1343 auto value = boost::const_pointer_cast<Element>(kv.second);
1344 if (value && value->getType() != Element::null) {
1345 if (element->contains(current_key) &&
1346 (value->getType() == Element::map ||
1347 value->getType() == Element::list)) {
1348 ElementPtr mutable_element = boost::const_pointer_cast<Element>(element->get(current_key));
1349 mergeDiffAdd(mutable_element, value, hierarchy,
1350 current_key, idx + 1, level - 1);
1351 } else {
1352 element->set(current_key, value);
1353 }
1354 }
1355 }
1356 return;
1357 }
1358 element = other;
1359}
1360
1361void
1363 HierarchyDescriptor& hierarchy, std::string key, size_t idx,
1364 unsigned level) {
1365 if (level == 0) {
1366 isc_throw(BadValue, "mergeDiffDel got infinite recursion: "
1367 "arguments include cycles");
1368 }
1369 if (element->getType() != other->getType()) {
1370 isc_throw(TypeError, "mergeDiffDel arguments not same type");
1371 }
1372
1373 if (element->getType() == Element::list) {
1374 for (auto const& value : other->listValue()) {
1375 ElementPtr mutable_right = boost::const_pointer_cast<Element>(value);
1376 for (uint32_t iter = 0; iter < element->listValue().size();) {
1377 bool removed = false;
1378 // Check if we have any description of the key in the
1379 // configuration hierarchy.
1380 auto f = hierarchy[idx].find(key);
1381 if (f != hierarchy[idx].end()) {
1382 ElementPtr mutable_left = boost::const_pointer_cast<Element>(element->listValue().at(iter));
1383 // Check if the elements refer to the same configuration
1384 // entity.
1385 if (f->second.match_(mutable_left, mutable_right)) {
1386 // Check if the user supplied data only contains
1387 // identification information, so the intent is to
1388 // delete the element, not just element data.
1389 if (f->second.no_data_(mutable_right)) {
1390 element->remove(iter);
1391 removed = true;
1392 } else {
1393 mergeDiffDel(mutable_left, mutable_right,
1394 hierarchy, key, idx, level - 1);
1395 if (mutable_left->empty()) {
1396 element->remove(iter);
1397 removed = true;
1398 }
1399 }
1400 }
1401 } else if (element->listValue().at(iter)->equals(*value)) {
1402 element->remove(iter);
1403 removed = true;
1404 }
1405 if (!removed) {
1406 ++iter;
1407 }
1408 }
1409 }
1410 return;
1411 }
1412
1413 if (element->getType() == Element::map) {
1414 // If the resulting element still contains data, we need to restore the
1415 // key parameters, so we store them here.
1416 ElementPtr new_elements = Element::createMap();
1417 for (auto const& kv : other->mapValue()) {
1418 auto current_key = kv.first;
1419 auto value = boost::const_pointer_cast<Element>(kv.second);
1420 if (value && value->getType() != Element::null) {
1421 if (element->contains(current_key)) {
1422 ElementPtr mutable_element = boost::const_pointer_cast<Element>(element->get(current_key));
1423 if (mutable_element->getType() == Element::map ||
1424 mutable_element->getType() == Element::list) {
1425 mergeDiffDel(mutable_element, value, hierarchy,
1426 current_key, idx + 1, level - 1);
1427 if (mutable_element->empty()) {
1428 element->remove(current_key);
1429 }
1430 } else {
1431 // Check if we have any description of the key in the
1432 // configuration hierarchy.
1433 auto f = hierarchy[idx].find(key);
1434 if (f != hierarchy[idx].end()) {
1435 // Check if the key is used for element
1436 // identification.
1437 if (f->second.is_key_(current_key)) {
1438 // Store the key parameter.
1439 new_elements->set(current_key, mutable_element);
1440 }
1441 }
1442 element->remove(current_key);
1443 }
1444 }
1445 }
1446 }
1447 // If the element still contains data, restore the key elements.
1448 if (element->size()) {
1449 for (auto const& kv : new_elements->mapValue()) {
1450 element->set(kv.first, kv.second);
1451 }
1452 }
1453 return;
1454 }
1455 element = ElementPtr(new NullElement());
1456}
1457
1458void
1459extend(const std::string& container, const std::string& extension,
1460 ElementPtr& element, ElementPtr& other, HierarchyDescriptor& hierarchy,
1461 std::string key, size_t idx, bool alter, unsigned level) {
1462
1463 if (level == 0) {
1464 isc_throw(BadValue, "extend got infinite recursion: "
1465 "arguments include cycles");
1466 }
1467 if (element->getType() != other->getType()) {
1468 isc_throw(TypeError, "extend arguments not same type");
1469 }
1470
1471 if (element->getType() == Element::list) {
1472 for (auto const& right : other->listValue()) {
1473 // Check if we have any description of the key in the configuration
1474 // hierarchy.
1475 auto f = hierarchy[idx].find(key);
1476 if (f != hierarchy[idx].end()) {
1477 ElementPtr mutable_right = boost::const_pointer_cast<Element>(right);
1478 for (auto const& left : element->listValue()) {
1479 ElementPtr mutable_left = boost::const_pointer_cast<Element>(left);
1480 if (container == key) {
1481 alter = true;
1482 }
1483 if (f->second.match_(mutable_left, mutable_right)) {
1484 extend(container, extension, mutable_left, mutable_right,
1485 hierarchy, key, idx, alter, level - 1);
1486 }
1487 }
1488 }
1489 }
1490 return;
1491 }
1492
1493 if (element->getType() == Element::map) {
1494 for (auto const& kv : other->mapValue()) {
1495 auto current_key = kv.first;
1496 auto value = boost::const_pointer_cast<Element>(kv.second);
1497 if (value && value->getType() != Element::null) {
1498 if (element->contains(current_key) &&
1499 (value->getType() == Element::map ||
1500 value->getType() == Element::list)) {
1501 ElementPtr mutable_element = boost::const_pointer_cast<Element>(element->get(current_key));
1502 if (container == key) {
1503 alter = true;
1504 }
1505 extend(container, extension, mutable_element, value,
1506 hierarchy, current_key, idx + 1, alter, level - 1);
1507 } else if (alter && current_key == extension) {
1508 element->set(current_key, value);
1509 }
1510 }
1511 }
1512 return;
1513 }
1514}
1515
1517copy(ConstElementPtr from, unsigned level) {
1518 if (!from) {
1519 isc_throw(BadValue, "copy got a null pointer");
1520 }
1521
1522 auto pos = from->getPosition();
1523 int from_type = from->getType();
1524 if (from_type == Element::integer) {
1525 return (ElementPtr(new IntElement(from->intValue(), pos)));
1526 } else if (from_type == Element::real) {
1527 return (ElementPtr(new DoubleElement(from->doubleValue(), pos)));
1528 } else if (from_type == Element::boolean) {
1529 return (ElementPtr(new BoolElement(from->boolValue(), pos)));
1530 } else if (from_type == Element::null) {
1531 return (ElementPtr(new NullElement()));
1532 } else if (from_type == Element::string) {
1533 return (ElementPtr(new StringElement(from->stringValue(), pos)));
1534 } else if (from_type == Element::list) {
1535 ElementPtr result = ElementPtr(new ListElement(pos));
1536 for (auto const& elem : from->listValue()) {
1537 if (level == 0) {
1538 result->add(elem);
1539 } else {
1540 result->add(copy(elem, level - 1));
1541 }
1542 }
1543 return (result);
1544 } else if (from_type == Element::map) {
1545 ElementPtr result = ElementPtr(new MapElement(pos));
1546 for (auto const& kv : from->mapValue()) {
1547 auto key = kv.first;
1548 auto value = kv.second;
1549 if (level == 0) {
1550 result->set(key, value);
1551 } else {
1552 result->set(key, copy(value, level - 1));
1553 }
1554 }
1555 return (result);
1556 } else {
1557 isc_throw(BadValue, "copy got an element of type: " << from_type);
1558 }
1559}
1560
1561namespace {
1562
1563// Helper function which blocks infinite recursion
1564bool
1565isEquivalent0(ConstElementPtr a, ConstElementPtr b, unsigned level) {
1566 // check looping forever on cycles
1567 if (!level) {
1568 isc_throw(BadValue, "isEquivalent got infinite recursion: "
1569 "arguments include cycles");
1570 }
1571 if (!a || !b) {
1572 isc_throw(BadValue, "isEquivalent got a null pointer");
1573 }
1574 // check types
1575 if (a->getType() != b->getType()) {
1576 return (false);
1577 }
1578 if (a->getType() == Element::list) {
1579 // check empty
1580 if (a->empty()) {
1581 return (b->empty());
1582 }
1583 // check size
1584 if (a->size() != b->size()) {
1585 return (false);
1586 }
1587
1588 // copy b into a list
1589 const size_t s = a->size();
1590 std::list<ConstElementPtr> l;
1591 for (size_t i = 0; i < s; ++i) {
1592 l.push_back(b->get(i));
1593 }
1594
1595 // iterate on a
1596 for (size_t i = 0; i < s; ++i) {
1597 ConstElementPtr item = a->get(i);
1598 // lookup this item in the list
1599 bool found = false;
1600 for (auto it = l.begin(); it != l.end(); ++it) {
1601 // if found in the list remove it
1602 if (isEquivalent0(item, *it, level - 1)) {
1603 found = true;
1604 l.erase(it);
1605 break;
1606 }
1607 }
1608 // if not found argument differs
1609 if (!found) {
1610 return (false);
1611 }
1612 }
1613
1614 // sanity check: the list must be empty
1615 if (!l.empty()) {
1616 isc_throw(Unexpected, "isEquivalent internal error");
1617 }
1618 return (true);
1619 } else if (a->getType() == Element::map) {
1620 // check sizes
1621 if (a->size() != b->size()) {
1622 return (false);
1623 }
1624 // iterate on the first map
1625 for (auto const& kv : a->mapValue()) {
1626 // get the b value for the given keyword and recurse
1627 ConstElementPtr item = b->get(kv.first);
1628 if (!item || !isEquivalent0(kv.second, item, level - 1)) {
1629 return (false);
1630 }
1631 }
1632 return (true);
1633 } else {
1634 return (a->equals(*b));
1635 }
1636}
1637
1638} // end anonymous namespace
1639
1640bool
1642 return (isEquivalent0(a, b, 100));
1643}
1644
1645namespace {
1646
1647void
1648prettyPrint0(ConstElementPtr element, std::ostream& out,
1649 unsigned indent, unsigned step, unsigned level) {
1650 if (level == 0) {
1651 isc_throw(BadValue, "prettyPrint got infinite recursion: "
1652 "arguments include cycles");
1653 }
1654 if (!element) {
1655 isc_throw(BadValue, "prettyPrint got a null pointer");
1656 }
1657 if (element->getType() == Element::list) {
1658 // empty list case
1659 if (element->empty()) {
1660 out << "[ ]";
1661 return;
1662 }
1663
1664 // complex ? multiline : oneline
1665 if (!element->get(0)) {
1666 isc_throw(BadValue, "prettyPrint got a null pointer");
1667 }
1668 int first_type = element->get(0)->getType();
1669 bool complex = false;
1670 if ((first_type == Element::list) || (first_type == Element::map)) {
1671 complex = true;
1672 }
1673 std::string separator = complex ? ",\n" : ", ";
1674
1675 // open the list
1676 out << "[" << (complex ? "\n" : " ");
1677
1678 // iterate on items
1679 auto const& l = element->listValue();
1680 bool first = true;
1681 for (auto const& it : l) {
1682 // add the separator if not the first item
1683 if (!first) {
1684 out << separator;
1685 } else {
1686 first = false;
1687 }
1688 // add indentation
1689 if (complex) {
1690 out << std::string(indent + step, ' ');
1691 }
1692 // recursive call
1693 prettyPrint0(it, out, indent + step, step, level - 1);
1694 }
1695
1696 // close the list
1697 if (complex) {
1698 out << "\n" << std::string(indent, ' ');
1699 } else {
1700 out << " ";
1701 }
1702 out << "]";
1703 } else if (element->getType() == Element::map) {
1704 // empty map case
1705 if (element->size() == 0) {
1706 out << "{ }";
1707 return;
1708 }
1709
1710 // open the map
1711 out << "{\n";
1712
1713 // iterate on keyword: value
1714 auto const& m = element->mapValue();
1715 bool first = true;
1716 for (auto const& it : m) {
1717 // add the separator if not the first item
1718 if (first) {
1719 first = false;
1720 } else {
1721 out << ",\n";
1722 }
1723 // add indentation
1724 out << std::string(indent + step, ' ');
1725 // add keyword:
1726 out << "\"" << it.first << "\": ";
1727 // recursive call
1728 prettyPrint0(it.second, out, indent + step, step, level - 1);
1729 }
1730
1731 // close the map
1732 out << "\n" << std::string(indent, ' ') << "}";
1733 } else {
1734 // not a list or a map
1735 element->toJSON(out);
1736 }
1737}
1738
1739} // end anonymous namespace
1740
1741void
1742prettyPrint(ConstElementPtr element, std::ostream& out,
1743 unsigned indent, unsigned step) {
1744 prettyPrint0(element, out, indent, step, Element::MAX_NESTING_LEVEL);
1745}
1746
1747std::string
1748prettyPrint(ConstElementPtr element, unsigned indent, unsigned step) {
1749 std::stringstream ss;
1750 prettyPrint(element, ss, indent, step);
1751 return (ss.str());
1752}
1753
1754void Element::preprocess(std::istream& in, std::stringstream& out) {
1755
1756 std::string line;
1757
1758 while (std::getline(in, line)) {
1759 // If this is a comments line, replace it with empty line
1760 // (so the line numbers will still match
1761 if (!line.empty() && line[0] == '#') {
1762 line = "";
1763 }
1764
1765 // getline() removes end line characters. Unfortunately, we need
1766 // it for getting the line numbers right (in case we report an
1767 // error.
1768 out << line;
1769 out << "\n";
1770 }
1771}
1772
1773namespace {
1774
1775// Type of arcs.
1776typedef std::set<ConstElementPtr> Arc;
1777
1778// Helper function walking on the supposed tree.
1779bool
1780IsCircular0(ConstElementPtr element, Arc arc) {
1781 // Sanity check.
1782 if (!element) {
1783 return (false);
1784 }
1785 auto type = element->getType();
1786 // Container?
1787 if ((type != Element::list) && (type != Element::map)) {
1788 return (false);
1789 }
1790 // Empty? A cycle requires at least one element.
1791 if (element->empty()) {
1792 return (false);
1793 }
1794 // In the arc?
1795 if (arc.count(element) > 0) {
1796 return (true);
1797 }
1798 // This requires to work on a copy of the arc but it should be small.
1799 arc.insert(element);
1800 if (type == Element::list) {
1801 for (auto const& it : element->listValue()) {
1802 if (IsCircular0(it, arc)) {
1803 return (true);
1804 }
1805 }
1806 return (false);
1807 }
1808 // The argument is a map.
1809 for (auto const& it : element->mapValue()) {
1810 if (IsCircular0(it.second, arc)) {
1811 return (true);
1812 }
1813 }
1814 return (false);
1815}
1816
1817} // end anonymous namespace
1818
1819bool
1821 return (IsCircular0(element, Arc()));
1822}
1823
1824unsigned
1825getNestDepth(ConstElementPtr element, unsigned max_depth) {
1826 if (max_depth == 0U) {
1827 return (0U);
1828 }
1829 if (!element) {
1830 return (0U);
1831 }
1832 unsigned ret = 1U;
1833 if (element->getType() == Element::list) {
1834 for (auto const& i : element->listValue()) {
1835 unsigned sub = getNestDepth(i, max_depth - 1);
1836 if (sub == max_depth - 1) {
1837 return (max_depth);
1838 }
1839 if (sub + 1 > ret) {
1840 ret = sub + 1;
1841 }
1842 }
1843 } else if (element->getType() == Element::map) {
1844 for (auto const& i : element->mapValue()) {
1845 unsigned sub = getNestDepth(i.second, max_depth - 1);
1846 if (sub == max_depth - 1) {
1847 return (max_depth);
1848 }
1849 if (sub + 1 > ret) {
1850 ret = sub + 1;
1851 }
1852 }
1853
1854 }
1855 return (ret);
1856}
1857
1858} // end of isc::data namespace
1859} // end of isc namespace
@ map
Definition data.h:160
@ list
Definition data.h:159
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
Wrapper over int128_t.
Definition data.h:793
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const override
Converts the Element to JSON format and appends it to the given stringstream.
Definition data.cc:900
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const override
Checks whether the other Element is equal.
Definition data.cc:1107
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1124
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:921
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:905
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1118
The Element class represents a piece of data, used by the command channel and configuration parts.
Definition data.h:73
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
virtual bool getValue(int64_t &t) const
Get the integer value.
Definition data.cc:123
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
Definition data.cc:709
virtual int64_t intValue() const
Return the integer value.
Definition data.h:275
static constexpr unsigned MAX_NESTING_LEVEL
Maximum nesting level of Element objects.
Definition data.h:86
std::string str() const
Returns a string representing the Element and all its child elements.
Definition data.cc:104
virtual std::string stringValue() const
Return the string value.
Definition data.h:295
std::string toWire() const
Returns the wireformat for the Element and all its child elements.
Definition data.cc:111
types
The types that an Element can hold.
Definition data.h:152
virtual void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const =0
Converts the Element to JSON format and appends it to the given output stream.
static ElementPtr fromWire(std::stringstream &in, int length)
These function parse the wireformat at the given stringstream (of the given length).
Definition data.cc:1061
virtual bool setValue(const long long int v)
Set the integer value.
Definition data.cc:153
static ElementPtr fromJSONFile(const std::string &file_name, bool preproc=false)
Reads contents of specified file and interprets it as JSON.
Definition data.cc:878
virtual bool empty() const
Return true if there are no elements in the list.
Definition data.cc:218
virtual void remove(const int i)
Removes the element at the given position.
Definition data.cc:208
virtual bool contains(const std::string &name) const
Checks if there is data at the given key.
Definition data.cc:238
virtual ConstElementPtr find(const std::string &identifier) const
Recursively finds any data at the given identifier.
Definition data.cc:243
virtual size_t size() const
Returns the number of elements in the list.
Definition data.cc:213
Element(types t, const Position &pos=ZERO_POSITION())
Constructor.
Definition data.h:181
virtual const std::map< std::string, ConstElementPtr > & mapValue() const
Return the map value.
Definition data.h:306
virtual void add(ElementPtr element)
Adds an ElementPtr to the list.
Definition data.cc:203
virtual const std::vector< ElementPtr > & listValue() const
Return the list value.
Definition data.h:300
virtual bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const =0
Test equality.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition data.cc:859
virtual ConstElementPtr get(const int i) const
Returns the ElementPtr at the given index.
Definition data.cc:188
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
static Element::types nameToType(const std::string &type_name)
Converts the string to the corresponding type Throws a TypeError if the name is unknown.
Definition data.cc:735
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
Definition data.cc:198
virtual double doubleValue() const
Return the double value.
Definition data.h:285
virtual isc::util::int128_t bigIntValue() const
Return the big integer value.
Definition data.h:280
types getType() const
Definition data.h:191
virtual bool boolValue() const
Return the boolean value.
Definition data.h:290
static void preprocess(std::istream &in, std::stringstream &out)
input text preprocessor.
Definition data.cc:1754
virtual ElementPtr getNonConst(const int i) const
returns element as non-const pointer.
Definition data.cc:193
void removeEmptyContainersRecursively(unsigned level=MAX_NESTING_LEVEL)
Remove all empty maps and lists from this Element and its descendants.
Definition data.cc:57
Notes: IntElement type is changed to int64_t.
Definition data.h:776
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:895
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1096
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
Definition data.h:50
void sort(std::string const &index=std::string())
Sorts the elements inside the list.
Definition data.cc:1163
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:983
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1141
ConstElementPtr find(const std::string &id) const override
Recursively finds any data at the given identifier.
Definition data.cc:1033
void set(const std::string &key, ConstElementPtr value) override
Sets the ElementPtr at the given key.
Definition data.cc:1077
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const override
Test equality.
Definition data.cc:1199
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const override
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:1004
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1130
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:930
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1135
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:935
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
Definition data.h:37
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError,...
Definition data.h:234
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool operator==(const Element &a, const Element &b)
Test equality.
Definition data.cc:268
void extend(const std::string &container, const std::string &extension, ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, bool alter, unsigned level)
Extends data by adding the specified 'extension' elements from 'other' inside the 'container' element...
Definition data.cc:1459
void removeIdentical(ElementPtr a, ConstElementPtr b)
Remove all values from the first ElementPtr that are equal in the second.
Definition data.cc:1230
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element. (on the first level).
Definition data.cc:1276
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
Definition data.cc:1641
bool operator<(Element const &a, Element const &b)
Test less than.
Definition data.cc:277
bool IsCircular(ConstElementPtr element)
Check if the data is circular.
Definition data.cc:1820
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition data.cc:1742
ElementPtr copy(ConstElementPtr from, unsigned level)
Copy the data up to a nesting level.
Definition data.cc:1517
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
void mergeDiffAdd(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, unsigned level)
Merges the diff data by adding the missing elements from 'other' to 'element' (recursively).
Definition data.cc:1294
unsigned getNestDepth(ConstElementPtr element, unsigned max_depth)
Compute the nesting depth.
Definition data.cc:1825
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a null pointer.
Definition data.cc:1225
std::ostream & operator<<(std::ostream &out, const Element::Position &pos)
Insert Element::Position as a string into stream.
Definition data.cc:51
void mergeDiffDel(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, unsigned level)
Merges the diff data by removing the data present in 'other' from 'element' (recursively).
Definition data.cc:1362
bool operator!=(const Element &a, const Element &b)
Test inequality.
Definition data.cc:272
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
std::vector< FunctionMap > HierarchyDescriptor
Hierarchy descriptor of the containers in a specific Element hierarchy tree.
Definition data.h:1119
boost::multiprecision::checked_int128_t int128_t
Definition bigints.h:19
Defines the logger used by the top-level component of kea-lfc.
Represents the position of the data element within a configuration string.
Definition data.h:107
uint32_t pos_
Position within the line.
Definition data.h:110
std::string str() const
Returns the position in the textual format.
Definition data.cc:44
uint32_t line_
Line number.
Definition data.h:109
std::string file_
File name.
Definition data.h:108