Index: /trunk/include/range_list.h
===================================================================
--- /trunk/include/range_list.h	(revision 112)
+++ /trunk/include/range_list.h	(revision 113)
@@ -176,3 +176,18 @@
 bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset);
 
+
+/* range_list_has_range():
+ *  Determines whether or not a specified range exists contiguously within the
+ *  range_list.
+ *
+ * Arguments:
+ *  rl     -- the range_list to search
+ *  start  -- the offset at the beginning of the range
+ *  length -- the length of the range
+ *
+ * Returns:
+ *  true if the specified range exists and is complete, false otherwise.
+ */
+bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length);
+
 #endif
Index: /trunk/lib/range_list.c
===================================================================
--- /trunk/lib/range_list.c	(revision 112)
+++ /trunk/lib/range_list.c	(revision 113)
@@ -320,2 +320,29 @@
   return true;
 }
+
+
+bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length)
+{
+  int32_t idx1, idx2;
+
+  idx1 = range_list_find(rl, start);
+  if(idx1 < 0)
+    return false;
+
+  idx2 = range_list_find(rl, start+length);
+  if(idx2 < 0)
+    return false;
+
+  if(idx1 == idx2)
+    return true;
+
+  while(idx1 != idx2)
+  {
+    if(rl->elements[idx1]->offset + rl->elements[idx1]->length 
+       != rl->elements[idx1+1]->offset)
+      return false;
+    idx1++;
+  }
+
+  return true;
+}
Index: /trunk/lib/regfi.c
===================================================================
--- /trunk/lib/regfi.c	(revision 112)
+++ /trunk/lib/regfi.c	(revision 113)
@@ -1665,5 +1665,5 @@
   if(ret_val->flag & VK_FLAG_NAME_PRESENT)
   {
-    if(ret_val->name_length + REGFI_VK_MIN_LENGTH > ret_val->cell_size)
+    if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size)
     {
       if(strict)
@@ -1673,11 +1673,11 @@
       }
       else
-	ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH;
+	ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4;
     }
 
     /* Round up to the next multiple of 8 */
-    length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8;
-    if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH)
-      length+=8;
+    cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8;
+    if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4)
+      cell_length+=8;
 
     ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1));
@@ -1687,6 +1687,5 @@
       return NULL;
     }
-    
-    /* Don't need to seek, should be at the right offset */
+
     length = ret_val->name_length;
     if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0)
@@ -1700,11 +1699,11 @@
   }
   else
-    length = REGFI_VK_MIN_LENGTH;
+    cell_length = REGFI_VK_MIN_LENGTH + 4;
 
   if(unalloc)
   {
     /* If cell_size is still greater, truncate. */
-    if(length < ret_val->cell_size)
-      ret_val->cell_size = length;
+    if(cell_length < ret_val->cell_size)
+      ret_val->cell_size = cell_length;
   }
 
@@ -1816,5 +1815,5 @@
 	break;
       
-      if((cell_len == 0) || ((cell_len & 0xFFFFFFFC) != cell_len))
+      if((cell_len == 0) || ((cell_len & 0xFFFFFFF8) != cell_len))
 	/* TODO: should report an error here. */
 	break;
Index: /trunk/src/reglookup-recover.c
===================================================================
--- /trunk/src/reglookup-recover.c	(revision 112)
+++ /trunk/src/reglookup-recover.c	(revision 113)
@@ -418,8 +418,14 @@
 
 
-bool removeRange(range_list* rl, uint32 rm_idx, uint32 offset, uint32 length)
-{
-  const range_list_element* cur_elem = range_list_get(rl, rm_idx);
-  
+bool removeRange(range_list* rl, uint32 offset, uint32 length)
+{
+  int32 rm_idx;
+  const range_list_element* cur_elem;
+  
+  rm_idx = range_list_find(rl, offset);
+  if(rm_idx < 0)
+    return false;
+
+  cur_elem = range_list_get(rl, rm_idx);
   if(cur_elem == NULL)
   {
@@ -481,5 +487,5 @@
 	}
 	
-	if(!removeRange(unalloc_cells, i, key->offset, key->cell_size))
+	if(!removeRange(unalloc_cells, key->offset, key->cell_size))
 	  return 30;
       }
@@ -499,5 +505,4 @@
   const range_list_element* cur_elem;
   uint32 i, j, num_keys, off, values_length, max_length;
-  int32 idx;
 
   num_keys=range_list_size(unalloc_keys);
@@ -529,6 +534,5 @@
 	if(nk->values != NULL)
 	{
-	  idx = range_list_find(unalloc_cells, off);
-	  if(idx < 0)
+	  if(!range_list_has_range(unalloc_cells, off, nk->cell_size))
 	  { /* We've parsed a values-list which isn't in the unallocated list,
 	     * so prune it. 
@@ -553,5 +557,6 @@
 	    if(values_length != (values_length & 0xFFFFFFF8))
 	      values_length = (values_length & 0xFFFFFFF8) + 8;
-	    if(!removeRange(unalloc_cells, idx, off, values_length))
+
+	    if(!removeRange(unalloc_cells, off, values_length))
 	      return 20;
 
@@ -560,6 +565,6 @@
 	      if(nk->values[j] != NULL)
 	      {
-		idx = range_list_find(unalloc_cells, nk->values[j]->offset);
-		if(idx < 0)
+		if(!range_list_has_range(unalloc_cells, nk->values[j]->offset, 
+					 nk->values[j]->cell_size))
 		{ /* We've parsed a value which isn't in the unallocated list,
 		   * so prune it.
@@ -575,16 +580,16 @@
 		   * and inspect data.
 		   */
-		  if(!removeRange(unalloc_cells, idx, nk->values[j]->offset,
+		  if(!removeRange(unalloc_cells, nk->values[j]->offset,
 				  nk->values[j]->cell_size))
 		    return 21;
 
 		  /* Don't bother pruning or removing from unalloc_cells if 
-		   * there is no data, if it is stored the offset.
+		   * there is no data, or it is stored in the offset.
 		   */
 		  if(nk->values[j]->data != NULL && !nk->values[j]->data_in_offset)
 		  {
 		    off = nk->values[j]->data_off+REGF_BLOCKSIZE;
-		    idx = range_list_find(unalloc_cells, off);
-		    if(idx < 0)
+		    if(!range_list_has_range(unalloc_cells, off, 
+					     nk->values[j]->data_size))
 		    { /* We've parsed a data cell which isn't in the unallocated 
 		       * list, so prune it.
@@ -595,5 +600,5 @@
 		    else
 		    { /*A data record was recovered. Remove from unalloc_cells.*/
-		      if(!removeRange(unalloc_cells, idx, off,
+		      if(!removeRange(unalloc_cells, off, 
 				      nk->values[j]->data_size))
 			return 22;
@@ -638,5 +643,5 @@
 	}
 	
-	if(!removeRange(unalloc_cells, i, vk->offset, vk->cell_size))
+	if(!removeRange(unalloc_cells, vk->offset, vk->cell_size))
 	  return 30;
       }
@@ -672,5 +677,5 @@
 	}
 	
-	if(!removeRange(unalloc_cells, i, sk->offset, sk->cell_size))
+	if(!removeRange(unalloc_cells, sk->offset, sk->cell_size))
 	  return 30;
       }
