buffer.h

Go to the documentation of this file.
00001 /*
00002  * buffer.h -- generic memory buffer.
00003  *
00004  * Copyright (c) 2005-2008, NLnet Labs. All rights reserved.
00005  *
00006  * See LICENSE for the license.
00007  *
00008  *
00009  * The buffer module implements a generic buffer.  The API is based on
00010  * the java.nio.Buffer interface.
00011  */
00012 
00013 #ifndef LDNS_BUFFER_H
00014 #define LDNS_BUFFER_H
00015 
00016 #include <assert.h>
00017 #include <stdarg.h>
00018 #include <string.h>
00019 
00020 #include <ldns/error.h>
00021 #include <ldns/common.h>
00022 
00023 #include "ldns/util.h"
00024 
00025 #ifdef __cplusplus
00026 extern "C" {
00027 #endif
00028 
00033 #define LDNS_MIN_BUFLEN 512
00034 
00050 struct ldns_struct_buffer
00051 {
00053         size_t   _position;
00054 
00056         size_t   _limit;
00057 
00059         size_t   _capacity;
00060 
00062         uint8_t *_data;
00063 
00065         unsigned _fixed : 1;
00066 
00070         ldns_status _status;
00071 };
00072 typedef struct ldns_struct_buffer ldns_buffer;
00073 
00074 
00075 #ifdef NDEBUG
00076 INLINE void
00077 ldns_buffer_invariant(ldns_buffer *ATTR_UNUSED(buffer))
00078 {
00079 }
00080 #else
00081 INLINE void
00082 ldns_buffer_invariant(ldns_buffer *buffer)
00083 {
00084         assert(buffer != NULL);
00085         assert(buffer->_position <= buffer->_limit);
00086         assert(buffer->_limit <= buffer->_capacity);
00087         assert(buffer->_data != NULL);
00088 }
00089 #endif
00090 
00097 ldns_buffer *ldns_buffer_new(size_t capacity);
00098 
00108 void ldns_buffer_new_frm_data(ldns_buffer *buffer, void *data, size_t size);
00109 
00115 INLINE void ldns_buffer_clear(ldns_buffer *buffer)
00116 {
00117         ldns_buffer_invariant(buffer);
00118 
00119         /* reset status here? */
00120 
00121         buffer->_position = 0;
00122         buffer->_limit = buffer->_capacity;
00123 }
00124 
00133 INLINE void ldns_buffer_flip(ldns_buffer *buffer)
00134 {
00135         ldns_buffer_invariant(buffer);
00136 
00137         buffer->_limit = buffer->_position;
00138         buffer->_position = 0;
00139 }
00140 
00146 INLINE void ldns_buffer_rewind(ldns_buffer *buffer)
00147 {
00148         ldns_buffer_invariant(buffer);
00149 
00150         buffer->_position = 0;
00151 }
00152 
00158 INLINE size_t
00159 ldns_buffer_position(ldns_buffer *buffer)
00160 {
00161         return buffer->_position;
00162 }
00163 
00170 INLINE void
00171 ldns_buffer_set_position(ldns_buffer *buffer, size_t mark)
00172 {
00173         assert(mark <= buffer->_limit);
00174         buffer->_position = mark;
00175 }
00176 
00184 INLINE void
00185 ldns_buffer_skip(ldns_buffer *buffer, ssize_t count)
00186 {
00187         assert(buffer->_position + count <= buffer->_limit);
00188         buffer->_position += count;
00189 }
00190 
00196 INLINE size_t
00197 ldns_buffer_limit(ldns_buffer *buffer)
00198 {
00199         return buffer->_limit;
00200 }
00201 
00208 INLINE void
00209 ldns_buffer_set_limit(ldns_buffer *buffer, size_t limit)
00210 {
00211         assert(limit <= buffer->_capacity);
00212         buffer->_limit = limit;
00213         if (buffer->_position > buffer->_limit)
00214                 buffer->_position = buffer->_limit;
00215 }
00216 
00222 INLINE size_t
00223 ldns_buffer_capacity(ldns_buffer *buffer)
00224 {
00225         return buffer->_capacity;
00226 }
00227 
00236 bool ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity);
00237 
00248 bool ldns_buffer_reserve(ldns_buffer *buffer, size_t amount);
00249 
00256 INLINE uint8_t *
00257 ldns_buffer_at(const ldns_buffer *buffer, size_t at)
00258 {
00259         assert(at <= buffer->_limit);
00260         return buffer->_data + at;
00261 }
00262 
00269 INLINE uint8_t *
00270 ldns_buffer_begin(const ldns_buffer *buffer)
00271 {
00272         return ldns_buffer_at(buffer, 0);
00273 }
00274 
00281 INLINE uint8_t *
00282 ldns_buffer_end(ldns_buffer *buffer)
00283 {
00284         return ldns_buffer_at(buffer, buffer->_limit);
00285 }
00286 
00292 INLINE uint8_t *
00293 ldns_buffer_current(ldns_buffer *buffer)
00294 {
00295         return ldns_buffer_at(buffer, buffer->_position);
00296 }
00297 
00305 INLINE size_t
00306 ldns_buffer_remaining_at(ldns_buffer *buffer, size_t at)
00307 {
00308         ldns_buffer_invariant(buffer);
00309         assert(at <= buffer->_limit);
00310         return buffer->_limit - at;
00311 }
00312 
00319 INLINE size_t
00320 ldns_buffer_remaining(ldns_buffer *buffer)
00321 {
00322         return ldns_buffer_remaining_at(buffer, buffer->_position);
00323 }
00324 
00334 INLINE int
00335 ldns_buffer_available_at(ldns_buffer *buffer, size_t at, size_t count)
00336 {
00337         return count <= ldns_buffer_remaining_at(buffer, at);
00338 }
00339 
00346 INLINE int
00347 ldns_buffer_available(ldns_buffer *buffer, size_t count)
00348 {
00349         return ldns_buffer_available_at(buffer, buffer->_position, count);
00350 }
00351 
00359 INLINE void
00360 ldns_buffer_write_at(ldns_buffer *buffer, size_t at, const void *data, size_t count)
00361 {
00362         assert(ldns_buffer_available_at(buffer, at, count));
00363         memcpy(buffer->_data + at, data, count);
00364 }
00365 
00372 INLINE void
00373 ldns_buffer_write(ldns_buffer *buffer, const void *data, size_t count)
00374 {
00375         ldns_buffer_write_at(buffer, buffer->_position, data, count);
00376         buffer->_position += count;
00377 }
00378 
00385 INLINE void
00386 ldns_buffer_write_string_at(ldns_buffer *buffer, size_t at, const char *str)
00387 {
00388         ldns_buffer_write_at(buffer, at, str, strlen(str));
00389 }
00390 
00396 INLINE void
00397 ldns_buffer_write_string(ldns_buffer *buffer, const char *str)
00398 {
00399         ldns_buffer_write(buffer, str, strlen(str));
00400 }
00401 
00408 INLINE void
00409 ldns_buffer_write_u8_at(ldns_buffer *buffer, size_t at, uint8_t data)
00410 {
00411         assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
00412         buffer->_data[at] = data;
00413 }
00414 
00420 INLINE void
00421 ldns_buffer_write_u8(ldns_buffer *buffer, uint8_t data)
00422 {
00423         ldns_buffer_write_u8_at(buffer, buffer->_position, data);
00424         buffer->_position += sizeof(data);
00425 }
00426 
00433 INLINE void
00434 ldns_buffer_write_u16_at(ldns_buffer *buffer, size_t at, uint16_t data)
00435 {
00436         assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
00437         ldns_write_uint16(buffer->_data + at, data);
00438 }
00439 
00445 INLINE void
00446 ldns_buffer_write_u16(ldns_buffer *buffer, uint16_t data)
00447 {
00448         ldns_buffer_write_u16_at(buffer, buffer->_position, data);
00449         buffer->_position += sizeof(data);
00450 }
00451 
00458 INLINE void
00459 ldns_buffer_write_u32_at(ldns_buffer *buffer, size_t at, uint32_t data)
00460 {
00461         assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
00462         ldns_write_uint32(buffer->_data + at, data);
00463 }
00464 
00470 INLINE void
00471 ldns_buffer_write_u32(ldns_buffer *buffer, uint32_t data)
00472 {
00473         ldns_buffer_write_u32_at(buffer, buffer->_position, data);
00474         buffer->_position += sizeof(data);
00475 }
00476 
00484 INLINE void
00485 ldns_buffer_read_at(ldns_buffer *buffer, size_t at, void *data, size_t count)
00486 {
00487         assert(ldns_buffer_available_at(buffer, at, count));
00488         memcpy(data, buffer->_data + at, count);
00489 }
00490 
00497 INLINE void
00498 ldns_buffer_read(ldns_buffer *buffer, void *data, size_t count)
00499 {
00500         ldns_buffer_read_at(buffer, buffer->_position, data, count);
00501         buffer->_position += count;
00502 }
00503 
00510 INLINE uint8_t
00511 ldns_buffer_read_u8_at(ldns_buffer *buffer, size_t at)
00512 {
00513         assert(ldns_buffer_available_at(buffer, at, sizeof(uint8_t)));
00514         return buffer->_data[at];
00515 }
00516 
00522 INLINE uint8_t
00523 ldns_buffer_read_u8(ldns_buffer *buffer)
00524 {
00525         uint8_t result = ldns_buffer_read_u8_at(buffer, buffer->_position);
00526         buffer->_position += sizeof(uint8_t);
00527         return result;
00528 }
00529 
00536 INLINE uint16_t
00537 ldns_buffer_read_u16_at(ldns_buffer *buffer, size_t at)
00538 {
00539         assert(ldns_buffer_available_at(buffer, at, sizeof(uint16_t)));
00540         return ldns_read_uint16(buffer->_data + at);
00541 }
00542 
00548 INLINE uint16_t
00549 ldns_buffer_read_u16(ldns_buffer *buffer)
00550 {
00551         uint16_t result = ldns_buffer_read_u16_at(buffer, buffer->_position);
00552         buffer->_position += sizeof(uint16_t);
00553         return result;
00554 }
00555 
00562 INLINE uint32_t
00563 ldns_buffer_read_u32_at(ldns_buffer *buffer, size_t at)
00564 {
00565         assert(ldns_buffer_available_at(buffer, at, sizeof(uint32_t)));
00566         return ldns_read_uint32(buffer->_data + at);
00567 }
00568 
00574 INLINE uint32_t
00575 ldns_buffer_read_u32(ldns_buffer *buffer)
00576 {
00577         uint32_t result = ldns_buffer_read_u32_at(buffer, buffer->_position);
00578         buffer->_position += sizeof(uint32_t);
00579         return result;
00580 }
00581 
00587 INLINE ldns_status
00588 ldns_buffer_status(ldns_buffer *buffer)
00589 {
00590         return buffer->_status;
00591 }
00592 
00598 INLINE bool
00599 ldns_buffer_status_ok(ldns_buffer *buffer)
00600 {
00601         if (buffer) {
00602                 return ldns_buffer_status(buffer) == LDNS_STATUS_OK;
00603         } else {
00604                 return false;
00605         }
00606 }
00607 
00614 int ldns_buffer_printf(ldns_buffer *buffer, const char *format, ...);
00615 /*      ATTR_FORMAT(printf, 2, 3);*/
00616 
00622 void ldns_buffer_free(ldns_buffer *buffer);
00623 
00630 void *ldns_buffer_export(ldns_buffer *buffer);
00631 
00639 void ldns_buffer_copy(ldns_buffer* result, ldns_buffer* from);
00640 
00641 #ifdef __cplusplus
00642 }
00643 #endif
00644 
00645 #endif /* LDNS_BUFFER_H */

Generated on Wed Dec 19 16:56:56 2012 for ldns by  doxygen 1.4.7