snprintf style fixed buffers

This commit is contained in:
Willem Toorop 2015-03-20 18:19:06 +01:00
parent a53f50b530
commit a5e8f22246
5 changed files with 58 additions and 12 deletions

View File

@ -914,6 +914,18 @@ getdns_pretty_print_dict(const struct getdns_dict *dict)
return ret;
} /* getdns_pretty_print_dict */
int
getdns_pretty_snprint_dict(char *str, size_t size, const getdns_dict *dict)
{
gldns_buffer buf;
if (!dict) return -1;
gldns_buffer_init_frm_data(&buf, str, size);
return getdns_pp_dict(&buf, 0, dict) < 0
? -1 : gldns_buffer_position(&buf);
}
char *
getdns_pretty_print_list(const getdns_list *list)
{
@ -936,6 +948,19 @@ getdns_pretty_print_list(const getdns_list *list)
return ret;
}
int
getdns_pretty_snprint_list(char *str, size_t size, const getdns_list *list)
{
gldns_buffer buf;
if (!list) return -1;
gldns_buffer_init_frm_data(&buf, str, size);
return getdns_pp_list(&buf, 0, list, 0) < 0
? -1 : gldns_buffer_position(&buf);
}
getdns_return_t
getdns_dict_remove_name(struct getdns_dict *this_dict, const char *name)
{

View File

@ -187,7 +187,14 @@ getdns_context_get_edns_version(getdns_context *context, uint8_t* value);
getdns_return_t
getdns_context_get_edns_do_bit(getdns_context *context, uint8_t* value);
char *getdns_pretty_print_list(const getdns_list *some_list);
int
getdns_pretty_snprint_dict(char *str, size_t size, const getdns_dict *dict);
char *
getdns_pretty_print_list(const getdns_list *some_list);
int
getdns_pretty_snprint_list(char *str, size_t size, const getdns_list *list);
#ifdef __cplusplus
}

View File

@ -74,7 +74,7 @@ gldns_buffer_set_capacity(gldns_buffer *buffer, size_t capacity)
void *data;
gldns_buffer_invariant(buffer);
assert(buffer->_position <= capacity);
assert(buffer->_position <= capacity && !buffer->_fixed);
data = (uint8_t *) realloc(buffer->_data, capacity);
if (!data) {
@ -126,7 +126,7 @@ gldns_buffer_printf(gldns_buffer *buffer, const char *format, ...)
if (written == -1) {
buffer->_status_err = 1;
return -1;
} else if ((size_t) written >= remaining) {
} else if (!buffer->_fixed && (size_t) written >= remaining) {
if (!gldns_buffer_reserve(buffer, (size_t) written + 1)) {
buffer->_status_err = 1;
return -1;

View File

@ -134,7 +134,7 @@ INLINE void
gldns_buffer_invariant(gldns_buffer *buffer)
{
assert(buffer != NULL);
assert(buffer->_position <= buffer->_limit);
assert(buffer->_position <= buffer->_limit || buffer->_fixed);
assert(buffer->_limit <= buffer->_capacity);
assert(buffer->_data != NULL);
}
@ -231,7 +231,7 @@ gldns_buffer_position(gldns_buffer *buffer)
INLINE void
gldns_buffer_set_position(gldns_buffer *buffer, size_t mark)
{
assert(mark <= buffer->_limit);
assert(mark <= buffer->_limit || buffer->_fixed);
buffer->_position = mark;
}
@ -245,7 +245,7 @@ gldns_buffer_set_position(gldns_buffer *buffer, size_t mark)
INLINE void
gldns_buffer_skip(gldns_buffer *buffer, ssize_t count)
{
assert(buffer->_position + count <= buffer->_limit);
assert(buffer->_position + count <= buffer->_limit || buffer->_fixed);
buffer->_position += count;
}
@ -317,7 +317,7 @@ int gldns_buffer_reserve(gldns_buffer *buffer, size_t amount);
INLINE uint8_t *
gldns_buffer_at(const gldns_buffer *buffer, size_t at)
{
assert(at <= buffer->_limit);
assert(at <= buffer->_limit || buffer->_fixed);
return buffer->_data + at;
}
@ -353,6 +353,7 @@ gldns_buffer_end(gldns_buffer *buffer)
INLINE uint8_t *
gldns_buffer_current(gldns_buffer *buffer)
{
assert(buffer->_position <= buffer->_limit || buffer->_fixed);
return gldns_buffer_at(buffer, buffer->_position);
}
@ -367,8 +368,7 @@ INLINE size_t
gldns_buffer_remaining_at(gldns_buffer *buffer, size_t at)
{
gldns_buffer_invariant(buffer);
assert(at <= buffer->_limit);
return buffer->_limit - at;
return at < buffer->_limit ? buffer->_limit - at : 0;
}
/**
@ -420,7 +420,15 @@ gldns_buffer_available(gldns_buffer *buffer, size_t count)
INLINE void
gldns_buffer_write_at(gldns_buffer *buffer, size_t at, const void *data, size_t count)
{
assert(gldns_buffer_available_at(buffer, at, count));
if (!buffer->_fixed)
assert(gldns_buffer_available_at(buffer, at, count));
else if (gldns_buffer_remaining_at(buffer, at) == 0)
return;
else if (count > gldns_buffer_remaining_at(buffer, at)) {
memcpy(buffer->_data + at, data,
gldns_buffer_remaining_at(buffer, at));
return;
}
memcpy(buffer->_data + at, data, count);
}
@ -469,6 +477,7 @@ gldns_buffer_write_string(gldns_buffer *buffer, const char *str)
INLINE void
gldns_buffer_write_u8_at(gldns_buffer *buffer, size_t at, uint8_t data)
{
if (buffer->_fixed && at + sizeof(data) > buffer->_limit) return;
assert(gldns_buffer_available_at(buffer, at, sizeof(data)));
buffer->_data[at] = data;
}
@ -494,6 +503,7 @@ gldns_buffer_write_u8(gldns_buffer *buffer, uint8_t data)
INLINE void
gldns_buffer_write_u16_at(gldns_buffer *buffer, size_t at, uint16_t data)
{
if (buffer->_fixed && at + sizeof(data) > buffer->_limit) return;
assert(gldns_buffer_available_at(buffer, at, sizeof(data)));
gldns_write_uint16(buffer->_data + at, data);
}
@ -519,6 +529,7 @@ gldns_buffer_write_u16(gldns_buffer *buffer, uint16_t data)
INLINE void
gldns_buffer_write_u32_at(gldns_buffer *buffer, size_t at, uint32_t data)
{
if (buffer->_fixed && at + sizeof(data) > buffer->_limit) return;
assert(gldns_buffer_available_at(buffer, at, sizeof(data)));
gldns_write_uint32(buffer->_data + at, data);
}

View File

@ -97,8 +97,6 @@ getdns_context_process_async
getdns_context_set_eventloop
getdns_context_detach_eventloop
getdns_context_run
plain_mem_funcs_user_arg
priv_getdns_context_mf
getdns_context_get_resolution_type
getdns_context_get_namespaces
getdns_context_get_dns_transport
@ -115,3 +113,8 @@ getdns_context_get_edns_maximum_udp_payload_size
getdns_context_get_edns_extended_rcode
getdns_context_get_edns_version
getdns_context_get_edns_do_bit
getdns_pretty_snprint_dict
getdns_pretty_print_list
getdns_pretty_snprint_list
plain_mem_funcs_user_arg
priv_getdns_context_mf