diff --git a/include/ndbuf/ndbuf.h b/include/ndbuf/ndbuf.h index ed1ff80..386413c 100644 --- a/include/ndbuf/ndbuf.h +++ b/include/ndbuf/ndbuf.h @@ -30,6 +30,7 @@ #define NDBUF_BURN (1 << 1) /* burn buffer data before free */ #define NDBUF_NREA (1 << 2) /* non reallocatable buffer */ #define NDBUF_UCMO (1 << 3) /* user defined memops functions */ +#define NDBUF_RORB (1 << 4) /* read-only buffer */ struct ndbuf_memops { void *(*alloc)(size_t); diff --git a/ndbuf.c b/ndbuf.c index 47c84bd..643a306 100644 --- a/ndbuf.c +++ b/ndbuf.c @@ -336,12 +336,25 @@ uint32_t ndbuf_write_u64(ndbuf_t *b, uint64_t uu) return sizeof(uint64_t); } +static inline int __ndbuf_is_ro(ndbuf_t *b) +{ + return (b->flags & NDBUF_RORB) ? 1 : 0; +} + +static inline int __ndbuf_is_nrea(ndbuf_t *b) +{ + return (b->flags & NDBUF_NREA) ? 1 : 0; +} + /* write raw data with the given length */ uint32_t ndbuf_write_raw(ndbuf_t *b, void *wi, uint32_t len) { if(!b || !b->raw) return 0; if(!wi || !len) return 0; + if(__ndbuf_is_ro(b)) return 0; /* read only buffer */ + if(b->ulength + len >= b->rlength) { + if(__ndbuf_is_nrea(b)) return 0; /* non reallocatable buffer */ if(__rdb_grow(b, len)) return 0; } @@ -361,7 +374,10 @@ uint32_t ndbuf_write_raw_head(ndbuf_t *b, void *wi, uint32_t len) if(!b || !b->raw) return 0; if(!wi || !len) return 0; + if(__ndbuf_is_ro(b)) return 0; /* read only buffer */ + if(b->ulength + len > b->rlength) { + if(__ndbuf_is_nrea(b)) return 0; /* non reallocatable buffer */ /* allocate a new one and copy it right */ if(b->rlength + len > NDBUF_MAXLENGTH) return -1; @@ -617,6 +633,8 @@ uint32_t ndbuf_print_va(ndbuf_t *b, const char *fmt, int argc, va_list ap) uint32_t len = 0, clen; int r, count; + if(__ndbuf_is_ro(b)) return 0; /* read only buffer */ + for(t = fmt, count = 0; *t != '\0'; t++, count++) { if(count > argc && argc != -1) return 0; switch(*t) {