19
19
namespace isc {
20
20
namespace util {
21
21
22
- // / @brief A standard DNS module exception that is thrown if an out-of-range
23
- // / buffer operation is being performed.
24
- // /
25
- class InvalidBufferPosition : public isc ::OutOfRange {
26
- public:
27
- InvalidBufferPosition (const char * file, size_t line, const char * what) :
28
- isc::OutOfRange (file, line, what) {}
29
- };
30
-
31
22
// / @brief The @c InputBuffer class is a buffer abstraction for
32
23
// / manipulating read-only data.
33
24
// /
@@ -114,26 +105,26 @@ class InputBuffer {
114
105
// / @brief Set the read position of the buffer to the given value.
115
106
// /
116
107
// / The new position must be in the valid range of the buffer;
117
- // / otherwise an exception of class @c
118
- // / isc::dns::InvalidBufferPosition will be thrown.
108
+ // / otherwise an exception of class @c isc::OutOfRange will be thrown.
119
109
// /
120
110
// / @param position The new position (offset from the beginning of
121
111
// / the buffer).
122
112
void setPosition (size_t position) {
123
113
if (base_ + position > end_) {
124
- throwError (" position is too large" );
114
+ isc_throw (OutOfRange,
115
+ " InputBuffer::setPosition position is too large" );
125
116
}
126
117
current_ = base_ + position;
127
118
}
128
119
129
120
// / @brief Peek an unsigned 8-bit integer from the buffer and return it.
130
121
// /
131
122
// / If the remaining length of the buffer is smaller than 8-bit,
132
- // / an exception of class @c isc::dns::InvalidBufferPosition will
133
- // / be thrown.
123
+ // / an exception of class @c isc::OutOfRange will be thrown.
134
124
uint8_t peekUint8 () {
135
125
if (current_ + sizeof (uint8_t ) > end_) {
136
- throwError (" read beyond end of buffer" );
126
+ isc_throw (OutOfRange,
127
+ " InputBuffer::peekUint8 read beyond end of buffer" );
137
128
}
138
129
139
130
return (*current_);
@@ -142,8 +133,7 @@ class InputBuffer {
142
133
// / @brief Read an unsigned 8-bit integer from the buffer and return it.
143
134
// /
144
135
// / If the remaining length of the buffer is smaller than 8-bit,
145
- // / an exception of class @c isc::dns::InvalidBufferPosition will
146
- // / be thrown.
136
+ // / an exception of class @c isc::OutOfRange will be thrown.
147
137
uint8_t readUint8 () {
148
138
uint8_t ret = peekUint8 ();
149
139
current_ += sizeof (uint8_t );
@@ -155,11 +145,11 @@ class InputBuffer {
155
145
// / from the buffer, and return it.
156
146
// /
157
147
// / If the remaining length of the buffer is smaller than 16-bit,
158
- // / an exception of class @c isc::dns::InvalidBufferPosition will
159
- // / be thrown.
148
+ // / an exception of class @c isc::OutOfRange will be thrown.
160
149
uint16_t peekUint16 () {
161
150
if (current_ + sizeof (uint16_t ) > end_) {
162
- throwError (" read beyond end of buffer" );
151
+ isc_throw (OutOfRange,
152
+ " InputBuffer::peekUint16 read beyond end of buffer" );
163
153
}
164
154
165
155
uint16_t ret;
@@ -173,8 +163,7 @@ class InputBuffer {
173
163
// / from the buffer, and return it.
174
164
// /
175
165
// / If the remaining length of the buffer is smaller than 16-bit,
176
- // / an exception of class @c isc::dns::InvalidBufferPosition will
177
- // / be thrown.
166
+ // / an exception of class @c isc::OutOfRange will be thrown.
178
167
uint16_t readUint16 () {
179
168
uint16_t ret = peekUint16 ();
180
169
current_ += sizeof (uint16_t );
@@ -186,11 +175,11 @@ class InputBuffer {
186
175
// / from the buffer, and return it.
187
176
// /
188
177
// / If the remaining length of the buffer is smaller than 32-bit,
189
- // / an exception of class @c isc::dns::InvalidBufferPosition will
190
- // / be thrown.
178
+ // / an exception of class @c isc::OutOfRange will be thrown.
191
179
uint32_t peekUint32 () {
192
180
if (current_ + sizeof (uint32_t ) > end_) {
193
- throwError (" read beyond end of buffer" );
181
+ isc_throw (OutOfRange,
182
+ " InputBuffer::peekUint32 read beyond end of buffer" );
194
183
}
195
184
196
185
uint32_t ret;
@@ -206,8 +195,7 @@ class InputBuffer {
206
195
// / from the buffer, and return it.
207
196
// /
208
197
// / If the remaining length of the buffer is smaller than 32-bit,
209
- // / an exception of class @c isc::dns::InvalidBufferPosition will
210
- // / be thrown.
198
+ // / an exception of class @c isc::OutOfRange will be thrown.
211
199
uint32_t readUint32 () {
212
200
uint32_t ret = peekUint32 ();
213
201
current_ += sizeof (uint32_t );
@@ -220,11 +208,12 @@ class InputBuffer {
220
208
// /
221
209
// / The data is copied as stored in the buffer; no conversion is
222
210
// / performed. If the remaining length of the buffer is smaller
223
- // / than the specified length, an exception of class @c
224
- // / isc::dns::InvalidBufferPosition will be thrown.
211
+ // / than the specified length, an exception of class @c isc::OutOfRange
212
+ // / will be thrown.
225
213
void peekData (void * data, size_t len) {
226
214
if (current_ + len > end_) {
227
- throwError (" read beyond end of buffer" );
215
+ isc_throw (OutOfRange,
216
+ " InputBuffer::peekData read beyond end of buffer" );
228
217
}
229
218
230
219
static_cast <void >(std::memmove (data, current_, len));
@@ -235,8 +224,8 @@ class InputBuffer {
235
224
// /
236
225
// / The data is copied as stored in the buffer; no conversion is
237
226
// / performed. If the remaining length of the buffer is smaller
238
- // / than the specified length, an exception of class @c
239
- // / isc::dns::InvalidBufferPosition will be thrown.
227
+ // / than the specified length, an exception of class @c isc::OutOfRange
228
+ // / will be thrown.
240
229
void readData (void * data, size_t len) {
241
230
peekData (data, len);
242
231
current_ += len;
@@ -245,13 +234,16 @@ class InputBuffer {
245
234
// / @brief Peek specified number of bytes as a vector.
246
235
// /
247
236
// / If specified buffer is too short, it will be expanded using
248
- // / vector::resize() method.
237
+ // / vector::resize() method. If the remaining length of the buffer
238
+ // / is smaller than the specified length, an exception of class
239
+ // / @c isc::OutOfRange will be thrown.
249
240
// /
250
241
// / @param data Reference to a buffer (data will be stored there).
251
242
// / @param len Size specified number of bytes to read in a vector.
252
243
void peekVector (std::vector<uint8_t >& data, size_t len) {
253
244
if (current_ + len > end_) {
254
- throwError (" read beyond end of buffer" );
245
+ isc_throw (OutOfRange,
246
+ " InputBuffer::peekVector read beyond end of buffer" );
255
247
}
256
248
257
249
data.resize (len);
@@ -261,7 +253,9 @@ class InputBuffer {
261
253
// / @brief Read specified number of bytes as a vector.
262
254
// /
263
255
// / If specified buffer is too short, it will be expanded using
264
- // / vector::resize() method.
256
+ // / vector::resize() method. If the remaining length of the buffer
257
+ // / is smaller than the specified length, an exception of class
258
+ // / @c isc::OutOfRange will be thrown.
265
259
// /
266
260
// / @param data Reference to a buffer (data will be stored there).
267
261
// / @param len Size specified number of bytes to read in a vector.
@@ -271,11 +265,6 @@ class InputBuffer {
271
265
}
272
266
273
267
private:
274
- // / @brief A common helper to throw an exception on invalid operation.
275
- static void throwError (const char * msg) {
276
- isc_throw (InvalidBufferPosition, msg);
277
- }
278
-
279
268
// / @brief Base of the buffer.
280
269
const uint8_t * base_;
281
270
@@ -433,8 +422,9 @@ class OutputBuffer {
433
422
// / @param pos The position in the buffer to be returned.
434
423
uint8_t operator [](size_t pos) const {
435
424
if (pos >= buffer_.size ()) {
436
- isc_throw (InvalidBufferPosition,
437
- " []: pos (" << pos << " ) >= size (" << buffer_.size () << " )" );
425
+ isc_throw (OutOfRange,
426
+ " OutputBuffer::[]: pos (" << pos
427
+ << " ) >= size (" << buffer_.size () << " )" );
438
428
}
439
429
return (buffer_[pos]);
440
430
}
@@ -467,7 +457,8 @@ class OutputBuffer {
467
457
// / @param len The length of data that should be trimmed.
468
458
void trim (size_t len) {
469
459
if (len > buffer_.size ()) {
470
- isc_throw (OutOfRange, " trimming too large from output buffer" );
460
+ isc_throw (OutOfRange,
461
+ " OutputBuffer::trim length too large from output buffer" );
471
462
}
472
463
buffer_.resize (buffer_.size () - len);
473
464
}
@@ -494,7 +485,8 @@ class OutputBuffer {
494
485
// / @param pos The position in the buffer to write the data.
495
486
void writeUint8At (uint8_t data, size_t pos) {
496
487
if (pos + sizeof (data) > buffer_.size ()) {
497
- isc_throw (InvalidBufferPosition, " write at invalid position" );
488
+ isc_throw (OutOfRange,
489
+ " OutputBuffer::writeUint8At write at invalid position" );
498
490
}
499
491
buffer_[pos] = data;
500
492
}
@@ -521,7 +513,8 @@ class OutputBuffer {
521
513
// / @param pos The beginning position in the buffer to write the data.
522
514
void writeUint16At (uint16_t data, size_t pos) {
523
515
if (pos + sizeof (data) > buffer_.size ()) {
524
- isc_throw (InvalidBufferPosition, " write at invalid position" );
516
+ isc_throw (OutOfRange,
517
+ " OutputBuffer::writeUint16At write at invalid position" );
525
518
}
526
519
527
520
buffer_[pos] = static_cast <uint8_t >((data & 0xff00U ) >> 8 );
0 commit comments