1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "character_string.h"
#include "rdata.h"
using namespace std;
using namespace isc::dns::rdata;
namespace isc {
namespace dns {
namespace {
bool isDigit(char c) {
return (('0' <= c) && (c <= '9'));
}
}
std::string
characterstr::getNextCharacterString(const std::string& input_str,
std::string::const_iterator& input_iterator)
{
string result;
// If the input string only contains white-spaces, it is an invalid
// <character-string>
if (input_iterator >= input_str.end()) {
isc_throw(InvalidRdataText, "Invalid text format, \
<character-string> field is missing.");
}
// Whether the <character-string> is separated with double quotes (")
bool quotes_separated = (*input_iterator == '"');
// Whether the quotes are pared if the string is quotes separated
bool quotes_paired = false;
if (quotes_separated) {
++input_iterator;
}
while(input_iterator < input_str.end()){
// Escaped characters processing
if (*input_iterator == '\\') {
if (input_iterator + 1 == input_str.end()) {
isc_throw(InvalidRdataText, "<character-string> ended \
prematurely.");
} else {
if (isDigit(*(input_iterator + 1))) {
// \DDD where each D is a digit. It its the octet
// corresponding to the decimal number described by DDD
if (input_iterator + 3 >= input_str.end()) {
isc_throw(InvalidRdataText, "<character-string> ended \
prematurely.");
} else {
int n = 0;
++input_iterator;
for (int i = 0; i < 3; ++i) {
if (isDigit(*input_iterator)) {
n = n*10 + (*input_iterator - '0');
++input_iterator;
} else {
isc_throw(InvalidRdataText, "Illegal decimal \
escaping series");
}
}
if (n > 255) {
isc_throw(InvalidRdataText, "Illegal octet \
number");
}
result.push_back(n);
continue;
}
} else {
++input_iterator;
result.push_back(*input_iterator);
++input_iterator;
continue;
}
}
}
if (quotes_separated) {
// If the <character-string> is seperated with quotes symbol and
// another quotes symbol is encountered, it is the end of the
// <character-string>
if (*input_iterator == '"') {
quotes_paired = true;
++input_iterator;
// Reach the end of character string
break;
}
} else if (*input_iterator == ' ') {
// If the <character-string> is not seperated with quotes symbol,
// it is seperated with <space> char
break;
}
result.push_back(*input_iterator);
++input_iterator;
}
if (result.size() > MAX_CHARSTRING_LEN) {
isc_throw(CharStringTooLong, "<character-string> is too long");
}
if (quotes_separated && !quotes_paired) {
isc_throw(InvalidRdataText, "The quotes are not paired");
}
return (result);
}
std::string
characterstr::getNextCharacterString(util::InputBuffer& buffer, size_t len) {
uint8_t str_len = buffer.readUint8();
size_t pos = buffer.getPosition();
if (len - pos < str_len) {
isc_throw(InvalidRdataLength, "Invalid string length");
}
uint8_t buf[MAX_CHARSTRING_LEN];
buffer.readData(buf, str_len);
return (string(buf, buf + str_len));
}
} // end of namespace dns
} // end of namespace isc
|