diff --git a/daemon/bencode.c b/daemon/bencode.c index d83e858e0..049c05969 100644 --- a/daemon/bencode.c +++ b/daemon/bencode.c @@ -134,6 +134,11 @@ alloc: return ret; } +bool bencode_buffer_contains(bencode_buffer_t *buf, const void *ptr) { + struct __bencode_buffer_piece *piece = buf->pieces; + return (ptr >= (void *) piece->buf && ptr < (void *) piece->tail); +} + void bencode_buffer_free(bencode_buffer_t *buf) { struct __bencode_buffer_piece *piece, *next; diff --git a/include/bencode.h b/include/bencode.h index f3c7f2beb..531175023 100644 --- a/include/bencode.h +++ b/include/bencode.h @@ -65,6 +65,9 @@ int bencode_buffer_init(bencode_buffer_t *buf); /* Allocate a piece of memory from the given buffer object */ void *bencode_buffer_alloc(bencode_buffer_t *, size_t); +// quick and non-reliable check whether the given pointer is from the buffer +bool bencode_buffer_contains(bencode_buffer_t *, const void *); + /* Destroys a previously initialized bencode_buffer_t object. All memory used by the object is freed * and all objects created through it become invalid. */ void bencode_buffer_free(bencode_buffer_t *buf);