chiark / gitweb /
dvd-sector-copy.c: Don't dump fake-bad-block search if no bad blocks.
[dvdrip] / dvd-sector-copy.c
index 177ce94f116577887bb351397dcdc5b51a028dcb..5caac23131087a443ba4574ce0c6eb016a5df40d 100644 (file)
@@ -409,30 +409,31 @@ static ssize_t read_sectors(secaddr pos, void *buf, secaddr want)
   struct badblock *bad, *best;
   unsigned char *p = buf;
 
-  best = 0; lo = 0; hi = badblocks.n;
+  if (badblocks.n) {
+    best = 0; lo = 0; hi = badblocks.n;
 #ifdef DEBUG
-  clear_progress();
-  printf(";; searching badblocks for %"PRIuSEC" .. %"PRIuSEC"\n",
-        pos, pos + want);
+    debug_clear_progress();
+    printf(";; searching badblocks for %"PRIuSEC" .. %"PRIuSEC"\n",
+          pos, pos + want);
 #endif
-  while (lo < hi) {
-    mid = lo + (hi - lo)/2; bad = &badblocks.v[mid];
+    while (lo < hi) {
+      mid = lo + (hi - lo)/2; bad = &badblocks.v[mid];
 #ifdef DEBUG
-    printf(";;   try %zu (%"PRIuSEC" .. %"PRIuSEC")... ",
-          mid, bad->start, bad->end);
+      printf(";;   try %zu (%"PRIuSEC" .. %"PRIuSEC")... ",
+            mid, bad->start, bad->end);
 #endif
-    if (pos < bad->start) { D( printf("high\n"); ) best = bad; hi = mid; }
-    else if (pos >= bad->end) { D( printf("low\n"); ) lo = mid + 1; }
-    else { D( printf("match!\n"); ) errno = EIO; return (-1); }
-  }
+      if (pos < bad->start) { D( printf("high\n"); ) best = bad; hi = mid; }
+      else if (pos >= bad->end) { D( printf("low\n"); ) lo = mid + 1; }
+      else { D( printf("match!\n"); ) errno = EIO; return (-1); }
+    }
 #ifdef DEBUG
-  if (best)
-    printf(";;   next is %"PRIuSEC" .. %"PRIuSEC"\n",
-          best->start, best->end);
+    if (best)
+      printf(";;   next is %"PRIuSEC" .. %"PRIuSEC"\n",
+            best->start, best->end);
 #endif
-  if (best && pos + want > best->start)
-    { want = best->start - pos; fakeerr = EIO; }
-
+    if (best && pos + want > best->start)
+      { want = best->start - pos; fakeerr = EIO; }
+  }
   done = 0;
   while (want) {
     if (vob)
@@ -531,7 +532,7 @@ static ssize_t recovery_read_sectors(struct recoverybuf *r,
 {
   ssize_t n;
 
-  assert(off + want <= r->sz);
+  assert(off <= r->sz); assert(want <= r->sz - off);
   n = read_sectors(pos, r->buf + off*SECTORSZ, want);
   return (n);
 }
@@ -611,7 +612,7 @@ static ssize_t recovery_read(struct recoverybuf *r,
           r->pos + pp, pp, nn);
 fflush(stdout);
 #endif
-    n = recovery_read_sectors(r, pos, pp, nn);
+    n = recovery_read_sectors(r, pos + pp, pp, nn);
 #ifdef DEBUG
     printf(" -> %zd\n", n);
 #endif
@@ -710,10 +711,12 @@ static ssize_t find_good_sector(secaddr *pos_inout, secaddr end,
     else bad_hi = pos + n + 1;
   }
   recovered(bad_lo, bad_hi); *pos_inout = good;
-  if (r.pos + r.start <= good && good <= r.pos + r.end) {
-    rearrange_sectors(&r, 0, good - r.pos, r.pos + r.end - good);
-  } else
+  if (good < r.pos + r.start || r.pos + r.end <= good)
     n = 0;
+  else {
+    n = r.pos + r.end - good;
+    rearrange_sectors(&r, 0, good - r.pos, n);
+  }
   return (n);
 }