/usr/include/ldns/buffer.h is in libldns-dev 1.6.17-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | /*
* buffer.h -- generic memory buffer.
*
* Copyright (c) 2005-2008, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*
* The buffer module implements a generic buffer. The API is based on
* the java.nio.Buffer interface.
*/
#ifndef LDNS_BUFFER_H
#define LDNS_BUFFER_H
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <ldns/error.h>
#include <ldns/common.h>
#include "ldns/util.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* number of initial bytes in buffer of
* which we cannot tell the size before hand
*/
#define LDNS_MIN_BUFLEN 512
/**
* \file buffer.h
*
* This file contains the definition of ldns_buffer, and functions to manipulate those.
*/
/**
* implementation of buffers to ease operations
*
* ldns_buffers can contain arbitrary information, per octet. You can write
* to the current end of a buffer, read from the current position, and
* access any data within it.
*
* Example use of buffers is in the source code of \ref host2str.c
*/
struct ldns_struct_buffer
{
/** The current position used for reading/writing */
size_t _position;
/** The read/write limit */
size_t _limit;
/** The amount of data the buffer can contain */
size_t _capacity;
/** The data contained in the buffer */
uint8_t *_data;
/** If the buffer is fixed it cannot be resized */
unsigned _fixed : 1;
/** The current state of the buffer. If writing to the buffer fails
* for any reason, this value is changed. This way, you can perform
* multiple writes in sequence and check for success afterwards. */
ldns_status _status;
};
typedef struct ldns_struct_buffer ldns_buffer;
#ifdef NDEBUG
INLINE void
ldns_buffer_invariant(ldns_buffer *ATTR_UNUSED(buffer))
{
}
#else
INLINE void
ldns_buffer_invariant(ldns_buffer *buffer)
{
assert(buffer != NULL);
assert(buffer->_position <= buffer->_limit);
assert(buffer->_limit <= buffer->_capacity);
assert(buffer->_data != NULL);
}
#endif
/**
* creates a new buffer with the specified capacity.
*
* \param[in] capacity the size (in bytes) to allocate for the buffer
* \return the created buffer
*/
ldns_buffer *ldns_buffer_new(size_t capacity);
/**
* creates a buffer with the specified data. The data IS copied
* and MEMORY allocations are done. The buffer is not fixed and can
* be resized using buffer_reserve().
*
* \param[in] buffer pointer to the buffer to put the data in
* \param[in] data the data to encapsulate in the buffer
* \param[in] size the size of the data
*/
void ldns_buffer_new_frm_data(ldns_buffer *buffer, void *data, size_t size);
/**
* clears the buffer and make it ready for writing. The buffer's limit
* is set to the capacity and the position is set to 0.
* \param[in] buffer the buffer to clear
*/
INLINE void ldns_buffer_clear(ldns_buffer *buffer)
{
ldns_buffer_invariant(buffer);
/* reset status here? */
buffer->_position = 0;
buffer->_limit = buffer->_capacity;
}
/**
* makes the buffer ready for reading the data that has been written to
* the buffer. The buffer's limit is set to the current position and
* the position is set to 0.
*
* \param[in] buffer the buffer to flip
* \return void
*/
INLINE void ldns_buffer_flip(ldns_buffer *buffer)
{
ldns_buffer_invariant(buffer);
buffer->_limit = buffer->_position;
buffer->_position = 0;
}
/**
* make the buffer ready for re-reading the data. The buffer's
* position is reset to 0.
* \param[in] buffer the buffer to rewind
*/
INLINE void ldns_buffer_rewind(ldns_buffer *buffer)
{
ldns_buffer_invariant(buffer);
buffer->_position = 0;
}
/**
* returns the current position in the buffer (as a number of bytes)
* \param[in] buffer the buffer
* \return the current position
*/
INLINE size_t
ldns_buffer_position(ldns_buffer *buffer)
{
return buffer->_position;
}
/**
* sets the buffer's position to MARK. The position must be less than
* or equal to the buffer's limit.
* \param[in] buffer the buffer
* \param[in] mark the mark to use
*/
INLINE void
ldns_buffer_set_position(ldns_buffer *buffer, size_t mark)
{
assert(mark <= buffer->_limit);
buffer->_position = mark;
}
/**
* changes the buffer's position by COUNT bytes. The position must not
* be moved behind the buffer's limit or before the beginning of the
* buffer.
* \param[in] buffer the buffer
* \param[in] count the count to use
*/
INLINE void
ldns_buffer_skip(ldns_buffer *buffer, ssize_t count)
{
assert(buffer->_position + count <= buffer->_limit);
buffer->_position += count;
}
/**
* returns the maximum size of the buffer
* \param[in] buffer
* \return the size
*/
INLINE size_t
ldns_buffer_limit(ldns_buffer *buffer)
{
return buffer->_limit;
}
/**
* changes the buffer's limit. If the buffer's position is greater
* than the new limit the position is set to the limit.
* \param[in] buffer the buffer
* \param[in] limit the new limit
*/
INLINE void
ldns_buffer_set_limit(ldns_buffer *buffer, size_t limit)
{
assert(limit <= buffer->_capacity);
buffer->_limit = limit;
if (buffer->_position > buffer->_limit)
buffer->_position = buffer->_limit;
}
/**
* returns the number of bytes the buffer can hold.
* \param[in] buffer the buffer
* \return the number of bytes
*/
INLINE size_t
ldns_buffer_capacity(ldns_buffer *buffer)
{
return buffer->_capacity;
}
/**
* changes the buffer's capacity. The data is reallocated so any
* pointers to the data may become invalid. The buffer's limit is set
* to the buffer's new capacity.
* \param[in] buffer the buffer
* \param[in] capacity the capacity to use
* \return whether this failed or succeeded
*/
bool ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity);
/**
* ensures BUFFER can contain at least AMOUNT more bytes. The buffer's
* capacity is increased if necessary using buffer_set_capacity().
*
* The buffer's limit is always set to the (possibly increased)
* capacity.
* \param[in] buffer the buffer
* \param[in] amount amount to use
* \return whether this failed or succeeded
*/
bool ldns_buffer_reserve(ldns_buffer *buffer, size_t amount);
/**
* returns a pointer to the data at the indicated position.
* \param[in] buffer the buffer
* \param[in] at position
* \return the pointer to the data
*/
INLINE uint8_t *
ldns_buffer_at(const ldns_buffer *buffer, size_t at)
{
assert(at <= buffer->_limit);
return buffer->_data + at;
}
/**
* returns a pointer to the beginning of the buffer (the data at
* position 0).
* \param[in] buffer the buffer
* \return the pointer
*/
INLINE uint8_t *
ldns_buffer_begin(const ldns_buffer *buffer)
{
return ldns_buffer_at(buffer, 0);
}
/**
* returns a pointer to the end of the buffer (the data at the buffer's
* limit).
* \param[in] buffer the buffer
* \return the pointer
*/
INLINE uint8_t *
ldns_buffer_end(ldns_buffer *buffer)
{
return ldns_buffer_at(buffer, buffer->_limit);
}
/**
* returns a pointer to the data at the buffer's current position.
* \param[in] buffer the buffer
* \return the pointer
*/
INLINE uint8_t *
ldns_buffer_current(ldns_buffer *buffer)
{
return ldns_buffer_at(buffer, buffer->_position);
}
/**
* returns the number of bytes remaining between the indicated position and
* the limit.
* \param[in] buffer the buffer
* \param[in] at indicated position
* \return number of bytes
*/
INLINE size_t
ldns_buffer_remaining_at(ldns_buffer *buffer, size_t at)
{
ldns_buffer_invariant(buffer);
assert(at <= buffer->_limit);
return buffer->_limit - at;
}
/**
* returns the number of bytes remaining between the buffer's position and
* limit.
* \param[in] buffer the buffer
* \return the number of bytes
*/
INLINE size_t
ldns_buffer_remaining(ldns_buffer *buffer)
{
return ldns_buffer_remaining_at(buffer, buffer->_position);
}
/**
* checks if the buffer has at least COUNT more bytes available.
* Before reading or writing the caller needs to ensure enough space
* is available!
* \param[in] buffer the buffer
* \param[in] at indicated position
* \param[in] count how much is available
* \return true or false (as int?)
*/
INLINE int
ldns_buffer_available_at(ldns_buffer *buffer, size_t at, size_t count)
{
return count <= ldns_buffer_remaining_at(buffer, at);
}
/**
* checks if the buffer has count bytes available at the current position
* \param[in] buffer the buffer
* \param[in] count how much is available
* \return true or false (as int?)
*/
INLINE int
ldns_buffer_available(ldns_buffer *buffer, size_t count)
{
return ldns_buffer_available_at(buffer, buffer->_position, count);
}
/**
* writes the given data to the buffer at the specified position
* \param[in] buffer the buffer
* \param[in] at the position (in number of bytes) to write the data at
* \param[in] data pointer to the data to write to the buffer
* \param[in] count the number of bytes of data to write
*/
INLINE void
ldns_buffer_write_at(ldns_buffer *buffer, size_t at, const void *data, size_t count)
{
assert(ldns_buffer_available_at(buffer, at, count));
memcpy(buffer->_data + at, data, count);
}
/**
* writes count bytes of data to the current position of the buffer
* \param[in] buffer the buffer
* \param[in] data the data to write
* \param[in] count the lenght of the data to write
*/
INLINE void
ldns_buffer_write(ldns_buffer *buffer, const void *data, size_t count)
{
ldns_buffer_write_at(buffer, buffer->_position, data, count);
buffer->_position += count;
}
/**
* copies the given (null-delimited) string to the specified position at the buffer
* \param[in] buffer the buffer
* \param[in] at the position in the buffer
* \param[in] str the string to write
*/
INLINE void
ldns_buffer_write_string_at(ldns_buffer *buffer, size_t at, const char *str)
{
ldns_buffer_write_at(buffer, at, str, strlen(str));
}
/**
* copies the given (null-delimited) string to the current position at the buffer
* \param[in] buffer the buffer
* \param[in] str the string to write
*/
INLINE void
ldns_buffer_write_string(ldns_buffer *buffer, const char *str)
{
ldns_buffer_write(buffer, str, strlen(str));
}
/**
* writes the given byte of data at the given position in the buffer
* \param[in] buffer the buffer
* \param[in] at the position in the buffer
* \param[in] data the 8 bits to write
*/
INLINE void
ldns_buffer_write_u8_at(ldns_buffer *buffer, size_t at, uint8_t data)
{
assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
buffer->_data[at] = data;
}
/**
* writes the given byte of data at the current position in the buffer
* \param[in] buffer the buffer
* \param[in] data the 8 bits to write
*/
INLINE void
ldns_buffer_write_u8(ldns_buffer *buffer, uint8_t data)
{
ldns_buffer_write_u8_at(buffer, buffer->_position, data);
buffer->_position += sizeof(data);
}
/**
* writes the given 2 byte integer at the given position in the buffer
* \param[in] buffer the buffer
* \param[in] at the position in the buffer
* \param[in] data the 16 bits to write
*/
INLINE void
ldns_buffer_write_u16_at(ldns_buffer *buffer, size_t at, uint16_t data)
{
assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
ldns_write_uint16(buffer->_data + at, data);
}
/**
* writes the given 2 byte integer at the current position in the buffer
* \param[in] buffer the buffer
* \param[in] data the 16 bits to write
*/
INLINE void
ldns_buffer_write_u16(ldns_buffer *buffer, uint16_t data)
{
ldns_buffer_write_u16_at(buffer, buffer->_position, data);
buffer->_position += sizeof(data);
}
/**
* writes the given 4 byte integer at the given position in the buffer
* \param[in] buffer the buffer
* \param[in] at the position in the buffer
* \param[in] data the 32 bits to write
*/
INLINE void
ldns_buffer_write_u32_at(ldns_buffer *buffer, size_t at, uint32_t data)
{
assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
ldns_write_uint32(buffer->_data + at, data);
}
/**
* writes the given 4 byte integer at the current position in the buffer
* \param[in] buffer the buffer
* \param[in] data the 32 bits to write
*/
INLINE void
ldns_buffer_write_u32(ldns_buffer *buffer, uint32_t data)
{
ldns_buffer_write_u32_at(buffer, buffer->_position, data);
buffer->_position += sizeof(data);
}
/**
* copies count bytes of data at the given position to the given data-array
* \param[in] buffer the buffer
* \param[in] at the position in the buffer to start
* \param[out] data buffer to copy to
* \param[in] count the length of the data to copy
*/
INLINE void
ldns_buffer_read_at(ldns_buffer *buffer, size_t at, void *data, size_t count)
{
assert(ldns_buffer_available_at(buffer, at, count));
memcpy(data, buffer->_data + at, count);
}
/**
* copies count bytes of data at the current position to the given data-array
* \param[in] buffer the buffer
* \param[out] data buffer to copy to
* \param[in] count the length of the data to copy
*/
INLINE void
ldns_buffer_read(ldns_buffer *buffer, void *data, size_t count)
{
ldns_buffer_read_at(buffer, buffer->_position, data, count);
buffer->_position += count;
}
/**
* returns the byte value at the given position in the buffer
* \param[in] buffer the buffer
* \param[in] at the position in the buffer
* \return 1 byte integer
*/
INLINE uint8_t
ldns_buffer_read_u8_at(ldns_buffer *buffer, size_t at)
{
assert(ldns_buffer_available_at(buffer, at, sizeof(uint8_t)));
return buffer->_data[at];
}
/**
* returns the byte value at the current position in the buffer
* \param[in] buffer the buffer
* \return 1 byte integer
*/
INLINE uint8_t
ldns_buffer_read_u8(ldns_buffer *buffer)
{
uint8_t result = ldns_buffer_read_u8_at(buffer, buffer->_position);
buffer->_position += sizeof(uint8_t);
return result;
}
/**
* returns the 2-byte integer value at the given position in the buffer
* \param[in] buffer the buffer
* \param[in] at position in the buffer
* \return 2 byte integer
*/
INLINE uint16_t
ldns_buffer_read_u16_at(ldns_buffer *buffer, size_t at)
{
assert(ldns_buffer_available_at(buffer, at, sizeof(uint16_t)));
return ldns_read_uint16(buffer->_data + at);
}
/**
* returns the 2-byte integer value at the current position in the buffer
* \param[in] buffer the buffer
* \return 2 byte integer
*/
INLINE uint16_t
ldns_buffer_read_u16(ldns_buffer *buffer)
{
uint16_t result = ldns_buffer_read_u16_at(buffer, buffer->_position);
buffer->_position += sizeof(uint16_t);
return result;
}
/**
* returns the 4-byte integer value at the given position in the buffer
* \param[in] buffer the buffer
* \param[in] at position in the buffer
* \return 4 byte integer
*/
INLINE uint32_t
ldns_buffer_read_u32_at(ldns_buffer *buffer, size_t at)
{
assert(ldns_buffer_available_at(buffer, at, sizeof(uint32_t)));
return ldns_read_uint32(buffer->_data + at);
}
/**
* returns the 4-byte integer value at the current position in the buffer
* \param[in] buffer the buffer
* \return 4 byte integer
*/
INLINE uint32_t
ldns_buffer_read_u32(ldns_buffer *buffer)
{
uint32_t result = ldns_buffer_read_u32_at(buffer, buffer->_position);
buffer->_position += sizeof(uint32_t);
return result;
}
/**
* returns the status of the buffer
* \param[in] buffer
* \return the status
*/
INLINE ldns_status
ldns_buffer_status(ldns_buffer *buffer)
{
return buffer->_status;
}
/**
* returns true if the status of the buffer is LDNS_STATUS_OK, false otherwise
* \param[in] buffer the buffer
* \return true or false
*/
INLINE bool
ldns_buffer_status_ok(ldns_buffer *buffer)
{
if (buffer) {
return ldns_buffer_status(buffer) == LDNS_STATUS_OK;
} else {
return false;
}
}
/**
* prints to the buffer, increasing the capacity if required using
* buffer_reserve(). The buffer's position is set to the terminating '\\0'
* Returns the number of characters written (not including the
* terminating '\\0') or -1 on failure.
*/
int ldns_buffer_printf(ldns_buffer *buffer, const char *format, ...);
/* ATTR_FORMAT(printf, 2, 3);*/
/**
* frees the buffer.
* \param[in] *buffer the buffer to be freed
* \return void
*/
void ldns_buffer_free(ldns_buffer *buffer);
/**
* Makes the buffer fixed and returns a pointer to the data. The
* caller is responsible for free'ing the result.
* \param[in] *buffer the buffer to be exported
* \return void
*/
void *ldns_buffer_export(ldns_buffer *buffer);
/**
* Copy contents of the from buffer to the result buffer and then flips
* the result buffer. Data will be silently truncated if the result buffer is
* too small.
* \param[out] *result resulting buffer which is copied to.
* \param[in] *from what to copy to result.
*/
void ldns_buffer_copy(ldns_buffer* result, ldns_buffer* from);
#ifdef __cplusplus
}
#endif
#endif /* LDNS_BUFFER_H */
|