Index: trunk/lib/regfi.c
===================================================================
--- trunk/lib/regfi.c	(revision 193)
+++ trunk/lib/regfi.c	(revision 199)
@@ -1848,6 +1848,6 @@
 
   /* XXX: this alloc/free of each sub key might be a bit excessive */
-  subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i);
-  while((subkey != NULL) && (found == false))
+  regfi_iterator_first_subkey(i);
+  while((subkey = regfi_iterator_cur_subkey(i)) != NULL && (found == false))
   {
     if(subkey->keyname != NULL 
@@ -1857,5 +1857,5 @@
     {
       talloc_unlink(NULL, subkey);
-      subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i);
+      regfi_iterator_next_subkey(i);
     }
   }
@@ -1918,8 +1918,10 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i)
+bool regfi_iterator_first_subkey(REGFI_ITERATOR* i)
 {
   i->cur_subkey = 0;
-  return regfi_iterator_cur_subkey(i);
+  
+  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE) 
+	  && (i->cur_subkey < i->cur_key->num_subkeys)); 
 }
 
@@ -1931,13 +1933,14 @@
   uint32_t nk_offset;
 
-  /* see if there is anything left to report */
-  if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE)
-      || (i->cur_subkey >= i->cur_key->num_subkeys))
-    return NULL;
-
-  nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
-
-  return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, 
-			i->string_encoding, true);
+  if((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE) 
+     && (i->cur_subkey < i->cur_key->num_subkeys))
+  {
+    nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
+
+    return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, 
+			  i->string_encoding, true);
+  }
+
+  return NULL;
 }
 
@@ -1945,16 +1948,10 @@
 /******************************************************************************
  *****************************************************************************/
-/* XXX: some way of indicating reason for failure should be added. */
-const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i)
-{
-  const REGFI_NK_REC* subkey;
-
+bool regfi_iterator_next_subkey(REGFI_ITERATOR* i)
+{
   i->cur_subkey++;
-  subkey = regfi_iterator_cur_subkey(i);
-
-  if(subkey == NULL)
-    i->cur_subkey--;
-
-  return subkey;
+
+  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE) 
+	  && (i->cur_subkey < i->cur_key->num_subkeys)); 
 }
 
@@ -1974,6 +1971,6 @@
     return false;
 
-  cur = regfi_iterator_first_value(i);
-  while((cur != NULL) && (found == false))
+  regfi_iterator_first_value(i);
+  while((cur = regfi_iterator_cur_value(i)) != NULL && (found == false))
   {
     if((cur->valuename != NULL)
@@ -1983,5 +1980,5 @@
     {
       regfi_free_record(cur);
-      cur = regfi_iterator_next_value(i);
+      regfi_iterator_next_value(i);
     }
   }
@@ -2000,8 +1997,9 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i)
+bool regfi_iterator_first_value(REGFI_ITERATOR* i)
 {
   i->cur_value = 0;
-  return regfi_iterator_cur_value(i);
+  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL 
+	  && (i->cur_value < i->cur_key->values->num_values));
 }
 
@@ -2014,12 +2012,10 @@
   uint32_t voffset;
 
-  if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL)
-  {
-    if(i->cur_value < i->cur_key->values->num_values)
-    {
-      voffset = i->cur_key->values->elements[i->cur_value];
-      ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, 
-				 i->string_encoding, true);
-    }
+  if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL 
+     && (i->cur_value < i->cur_key->values->num_values))
+  {
+    voffset = i->cur_key->values->elements[i->cur_value];
+    ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, 
+			       i->string_encoding, true);
   }
 
@@ -2030,14 +2026,9 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i)
-{
-  const REGFI_VK_REC* ret_val;
-
+bool regfi_iterator_next_value(REGFI_ITERATOR* i)
+{
   i->cur_value++;
-  ret_val = regfi_iterator_cur_value(i);
-  if(ret_val == NULL)
-    i->cur_value--;
-
-  return ret_val;
+  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL 
+	  && (i->cur_value < i->cur_key->values->num_values));
 }
 
