Index: trunk/lib/regfi.c
===================================================================
--- trunk/lib/regfi.c	(revision 203)
+++ trunk/lib/regfi.c	(revision 206)
@@ -1131,4 +1131,6 @@
     goto fail_locked;
 
+  ret_val->offset = offset;
+  ret_val->cell_size = cell_length;
   ret_val->num_values = num_values;
 
@@ -1175,5 +1177,5 @@
 }
 
-
+/* XXX: should give this boolean return type to indicate errors */
 void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, 
 			       REGFI_ENCODING output_encoding, bool strict)
@@ -1193,30 +1195,25 @@
   if(from_encoding == output_encoding)
   {
-    vk->valuename_raw = talloc_realloc(vk, vk->valuename_raw,
-					    uint8_t, vk->name_length+1);
-    vk->valuename_raw[vk->name_length] = '\0';
-    vk->valuename = (char*)vk->valuename_raw;
+    vk->name_raw[vk->name_length] = '\0';
+    vk->name = (char*)vk->name_raw;
   }
   else
   {
-    vk->valuename = talloc_array(vk, char, vk->name_length+1);
-    if(vk->valuename == NULL)
-    {
-      talloc_free(vk);
+    vk->name = talloc_array(vk, char, vk->name_length+1);
+    if(vk->name == NULL)
       return;
-    }
 
     tmp_size = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
 				  regfi_encoding_int2str(output_encoding),
-				  vk->valuename_raw, vk->valuename,
+				  vk->name_raw, vk->name,
 				  vk->name_length, vk->name_length+1);
     if(tmp_size < 0)
     {
       regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
-			" valuename to encoding %s.  Error message: %s",
+			" value name to encoding %s.  Error message: %s",
 			regfi_encoding_int2str(output_encoding), 
 			strerror(-tmp_size));
-      talloc_free(vk->valuename);
-      vk->valuename = NULL;
+      talloc_free(vk->name);
+      vk->name = NULL;
     }
   }
@@ -1227,5 +1224,5 @@
  ******************************************************************************/
 REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
-			       REGFI_ENCODING output_encoding, bool strict)
+			   REGFI_ENCODING output_encoding, bool strict)
 {
   REGFI_VK* ret_val = NULL;
@@ -1272,4 +1269,5 @@
 
 
+/* XXX: should give this boolean return type to indicate errors */
 void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, 
 			     REGFI_ENCODING output_encoding, bool strict)
@@ -1288,29 +1286,27 @@
   if(from_encoding == output_encoding)
   {
-    nk->keyname_raw = talloc_realloc(nk, nk->keyname_raw, uint8_t, nk->name_length+1);
-    nk->keyname_raw[nk->name_length] = '\0';
-    nk->keyname = (char*)nk->keyname_raw;
+    nk->name_raw[nk->name_length] = '\0';
+    nk->name = (char*)nk->name_raw;
   }
   else
   {
-    nk->keyname = talloc_array(nk, char, nk->name_length+1);
-    if(nk->keyname == NULL)
-    {
-      talloc_free(nk);
+    nk->name = talloc_array(nk, char, nk->name_length+1);
+    if(nk->name == NULL)
       return;
-    }
+
+    memset(nk->name,0,nk->name_length+1);
 
     tmp_size = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
 				  regfi_encoding_int2str(output_encoding),
-				  nk->keyname_raw, nk->keyname,
+				  nk->name_raw, nk->name,
 				  nk->name_length, nk->name_length+1);
     if(tmp_size < 0)
     {
       regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
-			" keyname to encoding %s.  Error message: %s",
+			" key name to encoding %s.  Error message: %s",
 			regfi_encoding_int2str(output_encoding), 
 			strerror(-tmp_size));
-      talloc_free(nk->keyname);
-      nk->keyname = NULL;
+      talloc_free(nk->name);
+      nk->name = NULL;
     }
   }
@@ -1322,5 +1318,5 @@
  ******************************************************************************/
 REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset,
-			     REGFI_ENCODING output_encoding, bool strict)
+			 REGFI_ENCODING output_encoding, bool strict)
 {
   REGFI_NK* nk;
@@ -1466,5 +1462,5 @@
  ******************************************************************************/
 REGFI_NK* regfi_find_root_nk(REGFI_FILE* file, const REGFI_HBIN* hbin, 
-				 REGFI_ENCODING output_encoding)
+			     REGFI_ENCODING output_encoding)
 {
   REGFI_NK* nk = NULL;
@@ -1510,5 +1506,5 @@
 /******************************************************************************
  ******************************************************************************/
-REGFI_FILE* regfi_alloc(int fd)
+REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding)
 {
   REGFI_FILE* ret_val;
@@ -1527,5 +1523,5 @@
   file_cb->seek = &regfi_raw_seek;
   
-  ret_val = regfi_alloc_cb(file_cb);
+  ret_val = regfi_alloc_cb(file_cb, output_encoding);
   if(ret_val == NULL)
     goto fail;
@@ -1557,5 +1553,6 @@
 /******************************************************************************
  ******************************************************************************/
-REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb)
+REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb, 
+			   REGFI_ENCODING output_encoding)
 {
   REGFI_FILE* rb;
@@ -1577,4 +1574,12 @@
   file_cb->seek(file_cb, 0, SEEK_SET);
 
+  if(output_encoding != REGFI_ENCODING_UTF8
+     && output_encoding != REGFI_ENCODING_ASCII)
+  { 
+    regfi_log_add(REGFI_LOG_ERROR, "Invalid output_encoding supplied"
+		  " in creation of regfi iterator.");
+    return NULL;
+  }
+
   /* Read file header */
   if ((rb = regfi_parse_regf(file_cb, false)) == NULL)
@@ -1585,4 +1590,5 @@
   rb->file_length = file_length;
   rb->cb = file_cb;
+  rb->string_encoding = output_encoding;
 
   if(pthread_mutex_init(&rb->cb_lock, NULL) != 0)
@@ -1664,5 +1670,5 @@
  * rest of the file if that fails.
  ******************************************************************************/
-REGFI_NK* regfi_rootkey(REGFI_FILE* file, REGFI_ENCODING output_encoding)
+REGFI_NK* regfi_rootkey(REGFI_FILE* file)
 {
   REGFI_NK* nk = NULL;
@@ -1674,5 +1680,5 @@
 
   root_offset = file->root_cell+REGFI_REGF_SIZE;
-  nk = regfi_load_key(file, root_offset, output_encoding, true);
+  nk = regfi_load_key(file, root_offset, file->string_encoding, true);
   if(nk != NULL)
   {
@@ -1696,5 +1702,5 @@
   {
     hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data;
-    nk = regfi_find_root_nk(file, hbin, output_encoding);
+    nk = regfi_find_root_nk(file, hbin, file->string_encoding);
   }
 
@@ -1717,23 +1723,14 @@
 /******************************************************************************
  *****************************************************************************/
-REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file, 
-				   REGFI_ENCODING output_encoding)
+REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file)
 {
   REGFI_NK* root;
   REGFI_ITERATOR* ret_val;
 
-  if(output_encoding != REGFI_ENCODING_UTF8
-     && output_encoding != REGFI_ENCODING_ASCII)
-  { 
-    regfi_log_add(REGFI_LOG_ERROR, "Invalid output_encoding supplied"
-		  " in creation of regfi iterator.");
-    return NULL;
-  }
-
   ret_val = talloc(NULL, REGFI_ITERATOR);
   if(ret_val == NULL)
     return NULL;
 
-  root = regfi_rootkey(file, output_encoding);
+  root = regfi_rootkey(file);
   if(root == NULL)
   {
@@ -1755,5 +1752,4 @@
   ret_val->cur_subkey = 0;
   ret_val->cur_value = 0;
-  ret_val->string_encoding = output_encoding;
     
   return ret_val;
@@ -1853,6 +1849,6 @@
   while((subkey = regfi_iterator_cur_subkey(i)) != NULL && (found == false))
   {
-    if(subkey->keyname != NULL 
-       && strcasecmp(subkey->keyname, subkey_name) == 0)
+    if(subkey->name != NULL 
+       && strcasecmp(subkey->name, subkey_name) == 0)
       found = true;
     else
@@ -1903,4 +1899,5 @@
 const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i)
 {
+  /* XXX: do we need to add a NULL talloc reference here? */
   return i->cur_key;
 }
@@ -1909,10 +1906,10 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_SK* regfi_iterator_cur_sk(REGFI_ITERATOR* i)
-{
-  if(i->cur_key == NULL || i->cur_key->sk_off == REGFI_OFFSET_NONE)
-    return NULL;
-
-  return regfi_load_sk(i->f, i->cur_key->sk_off + REGFI_REGF_SIZE, true);
+const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key)
+{
+  if(key == NULL || key->sk_off == REGFI_OFFSET_NONE)
+    return NULL;
+
+  return regfi_load_sk(file, key->sk_off + REGFI_REGF_SIZE, true);
 }
 
@@ -1941,5 +1938,5 @@
 
     return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, 
-			  i->string_encoding, true);
+			  i->f->string_encoding, true);
   }
 
@@ -1967,5 +1964,5 @@
   uint32_t old_value = i->cur_value;
 
-  /* XXX: cur->valuename can be NULL in the registry.  
+  /* XXX: cur->name can be NULL in the registry.  
    *      Should we allow for a way to search for that? 
    */
@@ -1976,6 +1973,6 @@
   while((cur = regfi_iterator_cur_value(i)) != NULL && (found == false))
   {
-    if((cur->valuename != NULL)
-       && (strcasecmp(cur->valuename, value_name) == 0))
+    if((cur->name != NULL)
+       && (strcasecmp(cur->name, value_name) == 0))
       found = true;
     else
@@ -2019,5 +2016,5 @@
     voffset = i->cur_key->values->elements[i->cur_value];
     ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, 
-			       i->string_encoding, true);
+			       i->f->string_encoding, true);
   }
 
@@ -2038,6 +2035,6 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_CLASSNAME* regfi_iterator_fetch_classname(REGFI_ITERATOR* i,
-						      const REGFI_NK* key)
+const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file,
+					     const REGFI_NK* key)
 {
   REGFI_CLASSNAME* ret_val;
@@ -2052,10 +2049,10 @@
 
   offset = key->classname_off + REGFI_REGF_SIZE;
-  max_size = regfi_calc_maxsize(i->f, offset);
+  max_size = regfi_calc_maxsize(file, offset);
   if(max_size <= 0)
     return NULL;
 
   parse_length = key->classname_length;
-  raw = regfi_parse_classname(i->f, offset, &parse_length, max_size, true);
+  raw = regfi_parse_classname(file, offset, &parse_length, max_size, true);
   
   if(raw == NULL)
@@ -2071,4 +2068,5 @@
     return NULL;
 
+  ret_val->offset = offset;
   ret_val->raw = raw;
   ret_val->size = parse_length;
@@ -2078,5 +2076,5 @@
 
   conv_size = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
-				 regfi_encoding_int2str(i->string_encoding),
+				 regfi_encoding_int2str(file->string_encoding),
 				 raw, interpreted,
 				 parse_length, parse_length);
@@ -2085,5 +2083,5 @@
     regfi_log_add(REGFI_LOG_WARN, "Error occurred while"
 		  " converting classname to charset %s.  Error message: %s",
-		  i->string_encoding, strerror(-conv_size));
+		  file->string_encoding, strerror(-conv_size));
     talloc_free(interpreted);
     ret_val->interpreted = NULL;
@@ -2102,6 +2100,6 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_DATA* regfi_iterator_fetch_data(REGFI_ITERATOR* i, 
-					    const REGFI_VK* value)
+const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file, 
+				   const REGFI_VK* value)
 {
   REGFI_DATA* ret_val = NULL;
@@ -2110,5 +2108,5 @@
   if(value->data_size != 0)
   {
-    raw_data = regfi_load_data(i->f, value->data_off, value->data_size,
+    raw_data = regfi_load_data(file, value->data_off, value->data_size,
 			      value->data_in_offset, true);
     if(raw_data.buf == NULL)
@@ -2132,5 +2130,6 @@
       }
 
-      if(!regfi_interpret_data(i->f, i->string_encoding, value->type, ret_val))
+      if(!regfi_interpret_data(file, file->string_encoding, 
+			       value->type, ret_val))
       {
 	regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
@@ -2344,6 +2343,6 @@
  *****************************************************************************/
 int32_t regfi_conv_charset(const char* input_charset, const char* output_charset,
-			 uint8_t* input, char* output, 
-			 uint32_t input_len, uint32_t output_max)
+			   uint8_t* input, char* output, 
+			   uint32_t input_len, uint32_t output_max)
 {
   iconv_t conv_desc;
@@ -2657,5 +2656,5 @@
   ret_val->name_length = SVAL(nk_header, 0x48);
   ret_val->classname_length = SVAL(nk_header, 0x4A);
-  ret_val->keyname = NULL;
+  ret_val->name = NULL;
 
   if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size)
@@ -2682,11 +2681,12 @@
   }
 
-  ret_val->keyname_raw = talloc_array(ret_val, uint8_t, ret_val->name_length);
-  if(ret_val->keyname_raw == NULL)
+  /* +1 to length in case we decided to use this directly as a string later */
+  ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1);
+  if(ret_val->name_raw == NULL)
     goto fail_locked;
 
   /* Don't need to seek, should be at the right offset */
   length = ret_val->name_length;
-  if((regfi_read(file->cb, (uint8_t*)ret_val->keyname_raw, &length) != 0)
+  if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0)
      || length != ret_val->name_length)
   {
@@ -2710,5 +2710,5 @@
 
 uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset, 
-			     uint16_t* name_length, uint32_t max_size, bool strict)
+			       uint16_t* name_length, uint32_t max_size, bool strict)
 {
   uint8_t* ret_val = NULL;
@@ -2811,6 +2811,6 @@
   ret_val->offset = offset;
   ret_val->cell_size = cell_length;
-  ret_val->valuename = NULL;
-  ret_val->valuename_raw = NULL;
+  ret_val->name = NULL;
+  ret_val->name_raw = NULL;
   
   if(ret_val->cell_size > max_size)
@@ -2867,10 +2867,11 @@
       cell_length+=8;
 
-    ret_val->valuename_raw = talloc_array(ret_val, uint8_t, ret_val->name_length);
-    if(ret_val->valuename_raw == NULL)
+    /* +1 to length in case we decided to use this directly as a string later */
+    ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1);
+    if(ret_val->name_raw == NULL)
       goto fail_locked;
 
     length = ret_val->name_length;
-    if((regfi_read(file->cb, (uint8_t*)ret_val->valuename_raw, &length) != 0)
+    if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0)
        || length != ret_val->name_length)
     {
