]> mj.ucw.cz Git - libucw.git/commitdiff
fastbufs: added a new function for safe reading of fixed-length array
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Fri, 1 Jun 2007 08:46:12 +0000 (10:46 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Fri, 1 Jun 2007 08:46:12 +0000 (10:46 +0200)
lib/fastbuf.c
lib/fastbuf.h

index dcb8d20dc0fe305b1229b2418ee89390bb4ae0ad..26df0c2f9afb17d202ac08d6809948b7bd633973 100644 (file)
@@ -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;
 }
 
index 68b2a9db18db938456fcefc309ccfb24a485ebcf..cd739ecbe33484f1a1518a625d806cf39882752e 100644 (file)
@@ -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)