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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (C) 2012-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <exceptions/exceptions.h>

#include <dns/exceptions.h>
#include <dns/rdata.h>
#include <dns/master_lexer.h>
#include <dns/char_string.h>
#include <util/buffer.h>

#include <boost/lexical_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <cctype><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <stdint.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace dns {
namespace rdata {
namespace generic {
namespace detail {

namespace {
// Convert a DDD form to the corresponding integer
int
decimalToNumber(const char* s, const char* s_end) {
    if (s_end - s < 3) {
        isc_throw(InvalidRdataText, "Escaped digits too short");
    }

    const std::string num_str(s, s + 3);
    try {
        const int i = boost::lexical_cast<int>(num_str);
        if (i > 255) {
            isc_throw(InvalidRdataText, "Escaped digits too large: "
                      << num_str);
        }
        return (i);
    } catch (const boost::bad_lexical_cast&) {
        isc_throw(InvalidRdataText,
                  "Invalid form for escaped digits: " << num_str);
    }
}
}

void
stringToCharString(const MasterToken::StringRegion& str_region,
                   CharString& result) {
    // make a space for the 1-byte length field; filled in at the end
    result.push_back(0);

    bool escape = false;
    const char* s = str_region.beg;
    const char* const s_end = str_region.beg + str_region.len;

    for (size_t n = str_region.len; n != 0; --n, ++s) {
        int c = (*s & 0xff);
        if (escape && std::isdigit(c) != 0) {
            c = decimalToNumber(s, s_end);
            // decimalToNumber() already throws if (s_end - s) is less
            // than 3. 'n' is an unsigned type (size_t) and can underflow.
            // 'n' and 's' are also updated by 1 in the for statement's
            // expression, so we update them by 2 instead of 3 here.
            n -= 2;
            s += 2;
        } else if (!escape && c == '\\') {
            escape = true;
            continue;
        }
        escape = false;
        result.push_back(c);
    }
    if (escape) {               // terminated by non-escaped '\'
        isc_throw(InvalidRdataText, "character-string ends with '\\'");
    }
    if (result.size() > MAX_CHARSTRING_LEN + 1) { // '+ 1' due to the len field
        isc_throw(CharStringTooLong, "character-string is too long: " <<
                  (result.size() - 1) << "(+1) characters");
    }
    result[0] = result.size() - 1;
}

void
stringToCharStringData(const MasterToken::StringRegion& str_region,
                       CharStringData& result) {
    bool escape = false;
    const char* s = str_region.beg;
    const char* const s_end = str_region.beg + str_region.len;

    for (size_t n = str_region.len; n != 0; --n, ++s) {
        int c = (*s & 0xff);
        if (escape && std::isdigit(c) != 0) {
            c = decimalToNumber(s, s_end);
            // decimalToNumber() already throws if (s_end - s) is less
            // than 3. 'n' is an unsigned type (size_t) and can underflow.
            // 'n' and 's' are also updated by 1 in the for statement's
            // expression, so we update them by 2 instead of 3 here.
            n -= 2;
            s += 2;
        } else if (!escape && c == '\\') {
            escape = true;
            continue;
        }
        escape = false;
        result.push_back(c);
    }
    if (escape) {               // terminated by non-escaped '\'
        isc_throw(InvalidRdataText, "character-string ends with '\\'");
    }
}

std::string
charStringToString(const CharString& char_string) {
    std::string s;
    bool first = true;
    for (auto const& it : char_string) {
        if (first) {
            first = false;
            continue;
        }
        const uint8_t ch = it;
        if ((ch < 0x20) || (ch >= 0x7f)) {
            // convert to escaped \xxx (decimal) format
            s.push_back('\\');
            s.push_back('0' + ((ch / 100) % 10));
            s.push_back('0' + ((ch / 10) % 10));
            s.push_back('0' + (ch % 10));
            continue;
        }
        if ((ch == '"') || (ch == ';') || (ch == '\\')) {
            s.push_back('\\');
        }
        s.push_back(ch);
    }

    return (s);
}

std::string
charStringDataToString(const CharStringData& char_string) {
    std::string s;
    for (auto const& it : char_string) {
        const uint8_t ch = it;
        if ((ch < 0x20) || (ch >= 0x7f)) {
            // convert to escaped \xxx (decimal) format
            s.push_back('\\');
            s.push_back('0' + ((ch / 100) % 10));
            s.push_back('0' + ((ch / 10) % 10));
            s.push_back('0' + (ch % 10));
            continue;
        }
        if ((ch == '"') || (ch == ';') || (ch == '\\')) {
            s.push_back('\\');
        }
        s.push_back(ch);
    }

    return (s);
}

int compareCharStrings(const detail::CharString& self,
                       const detail::CharString& other) {
    if (self.size() == 0 && other.size() == 0) {
        return (0);
    }
    if (self.size() == 0) {
        return (-1);
    }
    if (other.size() == 0) {
        return (1);
    }
    const size_t self_len = self[0];
    const size_t other_len = other[0];
    const size_t cmp_len = std::min(self_len, other_len);
    if (cmp_len == 0) {
        if (self_len < other_len) {
            return (-1);
        } else if (self_len > other_len) {
            return (1);
        } else {
            return (0);
        }
    }
    const int cmp = std::memcmp(&self[1], &other[1], cmp_len);
    if (cmp < 0) {
        return (-1);
    } else if (cmp > 0) {
        return (1);
    } else if (self_len < other_len) {
        return (-1);
    } else if (self_len > other_len) {
        return (1);
    } else {
        return (0);
    }
}

int compareCharStringDatas(const detail::CharStringData& self,
                           const detail::CharStringData& other) {
    if (self.size() == 0 && other.size() == 0) {
        return (0);
    }
    if (self.size() == 0) {
        return (-1);
    }
    if (other.size() == 0) {
        return (1);
    }
    const size_t self_len = self.size();
    const size_t other_len = other.size();
    const size_t cmp_len = std::min(self_len, other_len);
    const int cmp = std::memcmp(&self[0], &other[0], cmp_len);
    if (cmp < 0) {
        return (-1);
    } else if (cmp > 0) {
        return (1);
    } else if (self_len < other_len) {
        return (-1);
    } else if (self_len > other_len) {
        return (1);
    } else {
        return (0);
    }
}

size_t
bufferToCharString(isc::util::InputBuffer& buffer, size_t rdata_len,
                   CharString& target) {
    if (rdata_len < 1 || buffer.getLength() - buffer.getPosition() < 1) {
        isc_throw(isc::dns::DNSMessageFORMERR,
                  "insufficient data to read character-string length");
    }
    const uint8_t len = buffer.readUint8();
    if (rdata_len < len + 1) {
        isc_throw(isc::dns::DNSMessageFORMERR,
                  "character string length is too large: " <<
                  static_cast<int>(len));
    }
    if (buffer.getLength() - buffer.getPosition() < len) {
        isc_throw(isc::dns::DNSMessageFORMERR,
                  "not enough data in buffer to read character-string of len"
                  << static_cast<int>(len));
    }

    target.resize(len + 1);
    target[0] = len;
    buffer.readData(&target[0] + 1, len);

    return (len + 1);
}

} // end of detail
} // end of generic
} // end of rdata
} // end of dns
} // end of isc