From: Pavel Charvat Date: Fri, 1 Jun 2007 08:46:12 +0000 (+0200) Subject: fastbufs: added a new function for safe reading of fixed-length array X-Git-Tag: holmes-import~506^2~13^2~112 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=5fce46b8caeab8d51bf16cdc4eaa4bb8d23640dc;p=libucw.git fastbufs: added a new function for safe reading of fixed-length array --- 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)