Index: trunk/include/regfi.h
===================================================================
--- trunk/include/regfi.h	(revision 171)
+++ trunk/include/regfi.h	(revision 172)
@@ -1442,3 +1442,9 @@
 
 
+void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK_REC* nk, 
+			     REGFI_ENCODING output_encoding, bool strict);
+void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK_REC* vk, 
+			       REGFI_ENCODING output_encoding, bool strict);
+
+
 #endif	/* _REGFI_H */
Index: trunk/lib/regfi.c
===================================================================
--- trunk/lib/regfi.c	(revision 171)
+++ trunk/lib/regfi.c	(revision 172)
@@ -978,22 +978,7 @@
 
 
-
-/******************************************************************************
- ******************************************************************************/
-REGFI_VK_REC* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
+void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK_REC* vk, 
 			       REGFI_ENCODING output_encoding, bool strict)
 {
-  REGFI_VK_REC* ret_val = NULL;
-  int32_t max_size, tmp_size;
-  REGFI_ENCODING from_encoding;
-
-  max_size = regfi_calc_maxsize(file, offset);
-  if(max_size < 0)
-    return NULL;
-  
-  ret_val = regfi_parse_vk(file, offset, max_size, strict);
-  if(ret_val == NULL)
-    return NULL;
-
   /* XXX: Registry value names are supposedly limited to 16383 characters 
    *      according to:
@@ -1004,28 +989,28 @@
    *      when recovering deleted VK records.
    */
-
-  from_encoding = (ret_val->flags & REGFI_VK_FLAG_ASCIINAME)
+  int32_t tmp_size;
+  REGFI_ENCODING from_encoding = (vk->flags & REGFI_VK_FLAG_ASCIINAME)
     ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE;
 
   if(from_encoding == output_encoding)
   {
-    ret_val->valuename_raw = talloc_realloc(ret_val, ret_val->valuename_raw,
-					    uint8_t, ret_val->name_length+1);
-    ret_val->valuename_raw[ret_val->name_length] = '\0';
-    ret_val->valuename = (char*)ret_val->valuename_raw;
+    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;
   }
   else
   {
-    ret_val->valuename = talloc_array(ret_val, char, ret_val->name_length+1);
-    if(ret_val->valuename == NULL)
-    {
-      regfi_free_value(ret_val);
-      return NULL;
+    vk->valuename = talloc_array(vk, char, vk->name_length+1);
+    if(vk->valuename == NULL)
+    {
+      regfi_free_value(vk);
+      return;
     }
 
     tmp_size = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
 				  regfi_encoding_int2str(output_encoding),
-				  ret_val->valuename_raw, ret_val->valuename,
-				  ret_val->name_length, ret_val->name_length+1);
+				  vk->valuename_raw, vk->valuename,
+				  vk->name_length, vk->name_length+1);
     if(tmp_size < 0)
     {
@@ -1034,8 +1019,28 @@
 			regfi_encoding_int2str(output_encoding), 
 			strerror(-tmp_size));
-      talloc_free(ret_val->valuename);
-      ret_val->valuename = NULL;
-    }
-  }
+      talloc_free(vk->valuename);
+      vk->valuename = NULL;
+    }
+  }
+}
+
+
+/******************************************************************************
+ ******************************************************************************/
+REGFI_VK_REC* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
+			       REGFI_ENCODING output_encoding, bool strict)
+{
+  REGFI_VK_REC* ret_val = NULL;
+  int32_t max_size;
+
+  max_size = regfi_calc_maxsize(file, offset);
+  if(max_size < 0)
+    return NULL;
+  
+  ret_val = regfi_parse_vk(file, offset, max_size, strict);
+  if(ret_val == NULL)
+    return NULL;
+
+  regfi_interpret_valuename(file, ret_val, output_encoding, strict);
 
   return ret_val;
@@ -1069,28 +1074,7 @@
 
 
-
-/******************************************************************************
- *
- ******************************************************************************/
-REGFI_NK_REC* regfi_load_key(REGFI_FILE* file, uint32_t offset, 
+void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK_REC* nk, 
 			     REGFI_ENCODING output_encoding, bool strict)
 {
-  REGFI_NK_REC* nk;
-  uint32_t off;
-  int32_t max_size, tmp_size;
-  REGFI_ENCODING from_encoding;
-
-  max_size = regfi_calc_maxsize(file, offset);
-  if (max_size < 0) 
-    return NULL;
-
-  /* get the initial nk record */
-  if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL)
-  {
-    regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at"
-		      " offset 0x%.8X.", offset);
-    return NULL;
-  }
-
   /* XXX: Registry key names are supposedly limited to 255 characters according to:
    *      http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
@@ -1100,7 +1084,8 @@
    *      when recovering deleted NK records.
    */
-  from_encoding = (nk->flags & REGFI_NK_FLAG_ASCIINAME) 
+  int32_t tmp_size;
+  REGFI_ENCODING from_encoding = (nk->flags & REGFI_NK_FLAG_ASCIINAME) 
     ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE;
-
+  
   if(from_encoding == output_encoding)
   {
@@ -1115,5 +1100,5 @@
     {
       regfi_free_key(nk);
-      return NULL;
+      return;
     }
 
@@ -1132,5 +1117,30 @@
     }
   }
-
+}
+
+
+/******************************************************************************
+ *
+ ******************************************************************************/
+REGFI_NK_REC* regfi_load_key(REGFI_FILE* file, uint32_t offset, 
+			     REGFI_ENCODING output_encoding, bool strict)
+{
+  REGFI_NK_REC* nk;
+  uint32_t off;
+  int32_t max_size;
+
+  max_size = regfi_calc_maxsize(file, offset);
+  if (max_size < 0) 
+    return NULL;
+
+  /* get the initial nk record */
+  if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL)
+  {
+    regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at"
+		      " offset 0x%.8X.", offset);
+    return NULL;
+  }
+
+  regfi_interpret_keyname(file, nk, output_encoding, strict);
 
   /* get value list */
Index: trunk/src/common.c
===================================================================
--- trunk/src/common.c	(revision 171)
+++ trunk/src/common.c	(revision 172)
@@ -310,2 +310,29 @@
   return NULL;
 }
+
+
+static char* get_quoted_keyname(const REGFI_NK_REC* nk)
+{
+  char* ret_val;
+
+  if(nk->keyname == NULL)
+    ret_val = quote_buffer(nk->keyname_raw, nk->name_length, key_special_chars);
+  else
+    ret_val = quote_string(nk->keyname, key_special_chars);
+
+  return ret_val;
+}
+
+
+static char* get_quoted_valuename(const REGFI_VK_REC* vk)
+{
+  char* ret_val;
+
+  if(vk->valuename == NULL)
+    ret_val = quote_buffer(vk->valuename_raw, vk->name_length, 
+			   key_special_chars);
+  else
+    ret_val = quote_string(vk->valuename, key_special_chars);
+
+  return ret_val;
+}
Index: trunk/src/reglookup-recover.c
===================================================================
--- trunk/src/reglookup-recover.c	(revision 171)
+++ trunk/src/reglookup-recover.c	(revision 172)
@@ -79,5 +79,8 @@
   strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
 
-  quoted_name = quote_string(nk->keyname, key_special_chars);
+  /* XXX: Add command line option to choose output encoding */
+  regfi_interpret_keyname(f, nk, REGFI_ENCODING_ASCII, true);
+
+  quoted_name = get_quoted_keyname(nk);
   if (quoted_name == NULL)
   {
@@ -104,5 +107,5 @@
 
 
-void printValue(REGFI_FILE* f, const REGFI_VK_REC* vk, const char* prefix)
+void printValue(REGFI_FILE* f, REGFI_VK_REC* vk, const char* prefix)
 {
   char* quoted_value = NULL;
@@ -112,5 +115,8 @@
   const char* str_type = NULL;
 
-  quoted_name = quote_string(vk->valuename, key_special_chars);
+  /* XXX: Add command line option to choose output encoding */
+  regfi_interpret_valuename(f, vk, REGFI_ENCODING_ASCII, true);
+  
+  quoted_name = get_quoted_valuename(vk);
   if (quoted_name == NULL)
   { /* Value names are NULL when we're looking at the "(default)" value.
@@ -120,9 +126,9 @@
      * this value.
      */
-    quoted_name = malloc(1*sizeof(char));
+    quoted_name = strdup("");
     if(quoted_name == NULL)
       bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not allocate sufficient memory.\n");
-    quoted_name[0] = '\0';
-  }
+  }
+
   /* XXX: Add command line option to choose output encoding */
   if(vk->data != NULL 
@@ -278,7 +284,11 @@
 	path_element = talloc(path_stack, REGFI_BUFFER);
 	if(path_element != NULL)
-	  path_element->buf = (uint8_t*)quote_string(cur_ancestor->keyname, 
-						   key_special_chars);
+	{
+	  /* XXX: Add command line option to choose output encoding */
+	  regfi_interpret_keyname(f, cur_ancestor, REGFI_ENCODING_ASCII, true);
 	  
+	  path_element->buf = (uint8_t*)get_quoted_keyname(cur_ancestor);
+	}
+ 
 	if(path_element == NULL || path_element->buf == NULL 
 	   || !void_stack_push(path_stack, path_element))
@@ -929,5 +939,8 @@
     if(tmp_key->num_values > 0 && tmp_key->values != NULL)
     {
-      tmp_name = quote_string(tmp_key->keyname, key_special_chars);
+      /* XXX: Add command line option to choose output encoding */
+      regfi_interpret_keyname(f, tmp_key, REGFI_ENCODING_ASCII, true);
+
+      tmp_name = get_quoted_keyname(tmp_key);
       tmp_path = (char*)malloc(strlen(parent_paths[i])+strlen(tmp_name)+2);
       if(tmp_path == NULL)
Index: trunk/src/reglookup.c
===================================================================
--- trunk/src/reglookup.c	(revision 171)
+++ trunk/src/reglookup.c	(revision 172)
@@ -63,9 +63,5 @@
   struct tm* tmp_time_s = NULL;
 
-  if(vk->valuename == NULL)
-    quoted_name = quote_buffer(vk->valuename_raw, vk->name_length, 
-			       key_special_chars);
-  else
-    quoted_name = quote_string(vk->valuename, key_special_chars);
+  quoted_name = get_quoted_valuename(vk);
   if (quoted_name == NULL)
   { /* Value names are NULL when we're looking at the "(default)" value.
@@ -248,9 +244,5 @@
       tmp_key = cur->nk;
 
-    if(tmp_key->keyname == NULL)
-      name = quote_buffer(i->cur_key->keyname_raw, i->cur_key->name_length,
-			  key_special_chars);
-    else
-      name = quote_string(tmp_key->keyname, key_special_chars);
+    name = get_quoted_keyname(tmp_key);
 
     buf[buf_len-buf_left-1] = '/';
