Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

buffer.h

Go to the documentation of this file.
00001 /*
00002  * buffer.h -- generic memory buffer.
00003  *
00004  * Copyright (c) 2001-2004, 2005, 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 
00029 #define LDNS_MIN_BUFLEN 256
00030 
00034 struct ldns_struct_buffer
00035 {
00037         size_t   _position;
00038 
00040         size_t   _limit;
00041 
00043         size_t   _capacity;
00044 
00046         uint8_t *_data;
00047 
00049         unsigned _fixed : 1;
00050 
00052         ldns_status _status;
00053 };
00054 typedef struct ldns_struct_buffer ldns_buffer;
00055 
00056 
00057 #ifdef NDEBUG
00058 INLINE void
00059 ldns_buffer_invariant(ldns_buffer *ATTR_UNUSED(buffer))
00060 {
00061 }
00062 #else
00063 INLINE void
00064 ldns_buffer_invariant(ldns_buffer *buffer)
00065 {
00066         assert(buffer != NULL);
00067         assert(buffer->_position <= buffer->_limit);
00068         assert(buffer->_limit <= buffer->_capacity);
00069         assert(buffer->_data != NULL);
00070 }
00071 #endif
00072 
00079 ldns_buffer *ldns_buffer_new(size_t capacity);
00080 
00090 void ldns_buffer_new_frm_data(ldns_buffer *buffer, void *data, size_t size);
00091 
00096 void ldns_buffer_clear(ldns_buffer *buffer);
00097 
00106 void ldns_buffer_flip(ldns_buffer *buffer);
00107 
00112 void ldns_buffer_rewind(ldns_buffer *buffer);
00113 
00117 INLINE size_t
00118 ldns_buffer_position(ldns_buffer *buffer)
00119 {
00120         return buffer->_position;
00121 }
00122 
00127 INLINE void
00128 ldns_buffer_set_position(ldns_buffer *buffer, size_t mark)
00129 {
00130         assert(mark <= buffer->_limit);
00131         buffer->_position = mark;
00132 }
00133 
00139 INLINE void
00140 ldns_buffer_skip(ldns_buffer *buffer, ssize_t count)
00141 {
00142         assert(buffer->_position + count <= buffer->_limit);
00143         buffer->_position += count;
00144 }
00145 
00149 INLINE size_t
00150 ldns_buffer_limit(ldns_buffer *buffer)
00151 {
00152         return buffer->_limit;
00153 }
00154 
00159 INLINE void
00160 ldns_buffer_set_limit(ldns_buffer *buffer, size_t limit)
00161 {
00162         assert(limit <= buffer->_capacity);
00163         buffer->_limit = limit;
00164         if (buffer->_position > buffer->_limit)
00165                 buffer->_position = buffer->_limit;
00166 }
00167 
00171 INLINE size_t
00172 ldns_buffer_capacity(ldns_buffer *buffer)
00173 {
00174         return buffer->_capacity;
00175 }
00176 
00182 bool ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity);
00183 
00191 bool ldns_buffer_reserve(ldns_buffer *buffer, size_t amount);
00192 
00196 INLINE uint8_t *
00197 ldns_buffer_at(ldns_buffer *buffer, size_t at)
00198 {
00199         assert(at <= buffer->_limit);
00200         return buffer->_data + at;
00201 }
00202 
00207 INLINE uint8_t *
00208 ldns_buffer_begin(ldns_buffer *buffer)
00209 {
00210         return ldns_buffer_at(buffer, 0);
00211 }
00212 
00217 INLINE uint8_t *
00218 ldns_buffer_end(ldns_buffer *buffer)
00219 {
00220         return ldns_buffer_at(buffer, buffer->_limit);
00221 }
00222 
00226 INLINE uint8_t *
00227 ldns_buffer_current(ldns_buffer *buffer)
00228 {
00229         return ldns_buffer_at(buffer, buffer->_position);
00230 }
00231 
00236 INLINE size_t
00237 ldns_buffer_remaining_at(ldns_buffer *buffer, size_t at)
00238 {
00239         ldns_buffer_invariant(buffer);
00240         assert(at <= buffer->_limit);
00241         return buffer->_limit - at;
00242 }
00243 
00248 INLINE size_t
00249 ldns_buffer_remaining(ldns_buffer *buffer)
00250 {
00251         return ldns_buffer_remaining_at(buffer, buffer->_position);
00252 }
00253 
00259 INLINE int
00260 ldns_buffer_available_at(ldns_buffer *buffer, size_t at, size_t count)
00261 {
00262         return count <= ldns_buffer_remaining_at(buffer, at);
00263 }
00264 
00268 INLINE int
00269 ldns_buffer_available(ldns_buffer *buffer, size_t count)
00270 {
00271         return ldns_buffer_available_at(buffer, buffer->_position, count);
00272 }
00273 
00281 INLINE void
00282 ldns_buffer_write_at(ldns_buffer *buffer, size_t at, const void *data, size_t count)
00283 {
00284         assert(ldns_buffer_available_at(buffer, at, count));
00285         memcpy(buffer->_data + at, data, count);
00286 }
00287 
00291 INLINE void
00292 ldns_buffer_write(ldns_buffer *buffer, const void *data, size_t count)
00293 {
00294         ldns_buffer_write_at(buffer, buffer->_position, data, count);
00295         buffer->_position += count;
00296 }
00297 
00301 INLINE void
00302 ldns_buffer_write_string_at(ldns_buffer *buffer, size_t at, const char *str)
00303 {
00304         ldns_buffer_write_at(buffer, at, str, strlen(str));
00305 }
00306 
00310 INLINE void
00311 ldns_buffer_write_string(ldns_buffer *buffer, const char *str)
00312 {
00313         ldns_buffer_write(buffer, str, strlen(str));
00314 }
00315 
00319 INLINE void
00320 ldns_buffer_write_u8_at(ldns_buffer *buffer, size_t at, uint8_t data)
00321 {
00322         assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
00323         buffer->_data[at] = data;
00324 }
00325 
00329 INLINE void
00330 ldns_buffer_write_u8(ldns_buffer *buffer, uint8_t data)
00331 {
00332         ldns_buffer_write_u8_at(buffer, buffer->_position, data);
00333         buffer->_position += sizeof(data);
00334 }
00335 
00339 INLINE void
00340 ldns_buffer_write_u16_at(ldns_buffer *buffer, size_t at, uint16_t data)
00341 {
00342         assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
00343         ldns_write_uint16(buffer->_data + at, data);
00344 }
00345 
00349 INLINE void
00350 ldns_buffer_write_u16(ldns_buffer *buffer, uint16_t data)
00351 {
00352         ldns_buffer_write_u16_at(buffer, buffer->_position, data);
00353         buffer->_position += sizeof(data);
00354 }
00355 
00359 INLINE void
00360 ldns_buffer_write_u32_at(ldns_buffer *buffer, size_t at, uint32_t data)
00361 {
00362         assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
00363         ldns_write_uint32(buffer->_data + at, data);
00364 }
00365 
00369 INLINE void
00370 ldns_buffer_write_u32(ldns_buffer *buffer, uint32_t data)
00371 {
00372         ldns_buffer_write_u32_at(buffer, buffer->_position, data);
00373         buffer->_position += sizeof(data);
00374 }
00375 
00379 INLINE void
00380 ldns_buffer_read_at(ldns_buffer *buffer, size_t at, void *data, size_t count)
00381 {
00382         assert(ldns_buffer_available_at(buffer, at, count));
00383         memcpy(data, buffer->_data + at, count);
00384 }
00385 
00389 INLINE void
00390 ldns_buffer_read(ldns_buffer *buffer, void *data, size_t count)
00391 {
00392         ldns_buffer_read_at(buffer, buffer->_position, data, count);
00393         buffer->_position += count;
00394 }
00395 
00399 INLINE uint8_t
00400 ldns_buffer_read_u8_at(ldns_buffer *buffer, size_t at)
00401 {
00402         assert(ldns_buffer_available_at(buffer, at, sizeof(uint8_t)));
00403         return buffer->_data[at];
00404 }
00405 
00409 INLINE uint8_t
00410 ldns_buffer_read_u8(ldns_buffer *buffer)
00411 {
00412         uint8_t result = ldns_buffer_read_u8_at(buffer, buffer->_position);
00413         buffer->_position += sizeof(uint8_t);
00414         return result;
00415 }
00416 
00420 INLINE uint16_t
00421 ldns_buffer_read_u16_at(ldns_buffer *buffer, size_t at)
00422 {
00423         assert(ldns_buffer_available_at(buffer, at, sizeof(uint16_t)));
00424         return ldns_read_uint16(buffer->_data + at);
00425 }
00426 
00430 INLINE uint16_t
00431 ldns_buffer_read_u16(ldns_buffer *buffer)
00432 {
00433         uint16_t result = ldns_buffer_read_u16_at(buffer, buffer->_position);
00434         buffer->_position += sizeof(uint16_t);
00435         return result;
00436 }
00437 
00441 INLINE uint32_t
00442 ldns_buffer_read_u32_at(ldns_buffer *buffer, size_t at)
00443 {
00444         assert(ldns_buffer_available_at(buffer, at, sizeof(uint32_t)));
00445         return ldns_read_uint32(buffer->_data + at);
00446 }
00447 
00451 INLINE uint32_t
00452 ldns_buffer_read_u32(ldns_buffer *buffer)
00453 {
00454         uint32_t result = ldns_buffer_read_u32_at(buffer, buffer->_position);
00455         buffer->_position += sizeof(uint32_t);
00456         return result;
00457 }
00458 
00462 INLINE ldns_status
00463 ldns_buffer_status(ldns_buffer *buffer)
00464 {
00465         return buffer->_status;
00466 }
00467 
00471 INLINE bool
00472 ldns_buffer_status_ok(ldns_buffer *buffer)
00473 {
00474         if (buffer) {
00475                 return ldns_buffer_status(buffer) == LDNS_STATUS_OK;
00476         } else {
00477                 return false;
00478         }
00479 }
00480 
00487 int ldns_buffer_printf(ldns_buffer *buffer, const char *format, ...);
00488 /*      ATTR_FORMAT(printf, 2, 3);*/
00489 
00495 void ldns_buffer_free(ldns_buffer *buffer);
00496 
00503 void *ldns_buffer_export(ldns_buffer *buffer);
00504 
00505 #endif /* _LDNS_BUFFER_H_ */

Generated on Tue Jan 24 02:22:29 2006 for ldns by  doxygen 1.4.4