From 5fce46b8caeab8d51bf16cdc4eaa4bb8d23640dc Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Fri, 1 Jun 2007 10:46:12 +0200 Subject: [PATCH] fastbufs: added a new function for safe reading of fixed-length array --- lib/fastbuf.c | 7 +++++-- lib/fastbuf.h | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/fastbuf.c b/lib/fastbuf.c index dcb8d20d..26df0c2f 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -109,8 +109,11 @@ uns bread_slow(struct fastbuf *f, void *b, uns l, uns check) l -= k; total += k; } - if (check && total && l) - die("breadb: short read"); + if (check && l) + if (check == 2) + die("breada: short read"); + else if (total) + die("breadb: short read"); return total; } diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 68b2a9db..cd739ecb 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -235,6 +235,17 @@ static inline uns bread(struct fastbuf *f, void *b, uns l) return bread_slow(f, b, l, 0); } +static inline void breada(struct fastbuf *f, void *b, uns l) +{ + if (bavailr(f) >= l) + { + memcpy(b, f->bptr, l); + f->bptr += l; + } + else + bread_slow(f, b, l, 2); +} + static inline uns breadb(struct fastbuf *f, void *b, uns l) { if (bavailr(f) >= l) -- 2.39.2