We invent a new kind of buffer_if which is a readonly view of another
block of memory; such a view can be initialised with
buffer_view_readonly or buffer_view_clone. This makes a convenient
function to allow reparsing a packet, or using the buffer machinery to
parse a particular existing block of memory.
Also, make buffer_assert_free and buffer_assert_used actually call
assert (as well as logging the buffer ownership). So when these fail
(a) we don't attempt the clean teardown, and (b) we get a core dump if
those are enabled.
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
int line)
{
if (!buffer->free) {
int line)
{
if (!buffer->free) {
- fatal("BUF_ASSERT_FREE, %s line %d, owned by %s",
- file,line,buffer->owner);
+ fprintf(stderr,"secnet: BUF_ASSERT_FREE, %s line %d, owned by %s",
+ file,line,buffer->owner);
+ assert(!"buffer_assert_free failure");
int line)
{
if (buffer->free) {
int line)
{
if (buffer->free) {
- fatal("BUF_ASSERT_USED, %s line %d, last owned by %s",
- file,line,buffer->owner);
+ fprintf(stderr,"secnet: BUF_ASSERT_USED, %s line %d, last owned by %s",
+ file,line,buffer->owner);
+ assert(!"buffer_assert_used failure");
buf->base=safe_malloc(len,"buffer_new");
}
buf->base=safe_malloc(len,"buffer_new");
}
+void buffer_readonly_view(struct buffer_if *buf, const void *data, int32_t len)
+{
+ buf->free=False;
+ buf->owner="READONLY";
+ buf->flags=0;
+ buf->loc.file=NULL;
+ buf->loc.line=0;
+ buf->size=buf->len=len;
+ buf->base=buf->start=(uint8_t*)data;
+}
+
+void buffer_readonly_clone(struct buffer_if *out, const struct buffer_if *in)
+{
+ buffer_readonly_view(out,in->start,in->size);
+}
+
void buffer_copy(struct buffer_if *dst, const struct buffer_if *src)
{
if (dst->len < src->len) {
void buffer_copy(struct buffer_if *dst, const struct buffer_if *src)
{
if (dst->len < src->len) {
extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
+extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
+extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
+ /* Caller must only use unappend, unprepend et al. on n.
+ * New buffer state (in n) before this can be undefined. After use,
+ * it must NOT be freed. */
+
extern void buf_append_string(struct buffer_if *buf, cstring_t s);
extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
extern void buf_append_string(struct buffer_if *buf, cstring_t s);
extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);