Index: /trunk/src/reglookup.c
===================================================================
--- /trunk/src/reglookup.c	(revision 65)
+++ /trunk/src/reglookup.c	(revision 66)
@@ -43,5 +43,8 @@
 
 /* Other globals */
-const char* special_chars = ",\"\\";
+const char* key_special_chars = ",\"\\/";
+const char* subfield_special_chars = ",\"\\|";
+const char* common_special_chars = ",\"\\";
+
 iconv_t conv_desc;
 
@@ -150,6 +153,7 @@
   {
     iconv_close(conv_desc);
-    return -1;
-  }
+    return -errno;
+  }
+  *outbuf = '\0';
 
   iconv_close(conv_desc);  
@@ -173,10 +177,12 @@
   unsigned int ascii_max, cur_str_max;
   unsigned int str_rem, cur_str_rem, alen;
+  int ret_err;
 
   switch (type) 
   {
+  case REG_SZ:
+  case REG_EXPAND_SZ:
     /* REG_LINK is a symbolic link, stored as a unicode string. */
   case REG_LINK:
-  case REG_SZ:
     ascii_max = sizeof(char)*len;
     ascii = malloc(ascii_max+4);
@@ -184,29 +190,22 @@
       return NULL;
     
-    if(uni_to_ascii(datap, ascii, len, ascii_max) < 0)
-    {
-      free(ascii);
-      return NULL;
-    }
-    cur_quoted = quote_string(ascii, special_chars);
-    free(ascii);
-    return cur_quoted;
-    break;
-
-    /* XXX: This could be Unicode or ASCII.  Are we processing it 
-     *      correctly in both cases? 
+    /* Sometimes values have binary stored in them.  If the unicode
+     * conversion fails, just quote it raw.
      */
-  case REG_EXPAND_SZ:
-    ascii_max = sizeof(char)*len;
-    ascii = malloc(ascii_max+2);
-    if(ascii == NULL)
-      return NULL;
-
-    if(uni_to_ascii(datap, ascii, len, ascii_max) < 0)
-    {
-      free(ascii);
-      return NULL;
-    }
-    cur_quoted = quote_string(ascii, special_chars);
+    
+    ret_err = uni_to_ascii(datap, ascii, len, ascii_max);
+    if(ret_err < 0)
+    {
+      /* XXX: It would be nice if somehow we could include the name of this
+       *      value in the error message.
+       */
+      if(print_verbose)
+	fprintf(stderr, "VERBOSE: Unicode conversion failed; "
+		"printing as binary.  Error: %s\n", strerror(-ret_err));
+
+      cur_quoted = quote_buffer(datap, len, common_special_chars);
+    }
+    else
+      cur_quoted = quote_string(ascii, common_special_chars);
     free(ascii);
     return cur_quoted;
@@ -269,13 +268,20 @@
       if(num_nulls == 2)
       {
-	if(uni_to_ascii(cur_str, cur_ascii, cur_str_max, cur_str_max) < 0)
+	ret_err = uni_to_ascii(cur_str, cur_ascii, cur_str_len-1, cur_str_max);
+	if(ret_err < 0)
 	{
-	  free(ascii);
-	  free(cur_ascii);
-	  free(cur_str);
-	  return NULL;
+	  /* XXX: It would be nice if somehow we could include the name of this
+	   *      value in the error message.
+	   */
+	  if(print_verbose)
+	    fprintf(stderr, "VERBOSE: Unicode conversion failed on sub-field; "
+		    "printing as binary.  Error: %s\n", strerror(-ret_err));
+	  
+	  cur_quoted = quote_buffer(cur_str, cur_str_len-1, 
+				    subfield_special_chars);
 	}
-
-	cur_quoted = quote_string(cur_ascii, ",|\"\\");
+	else
+	  cur_quoted = quote_string(cur_ascii, subfield_special_chars);
+
 	alen = snprintf(asciip, str_rem, "%s", cur_quoted);
 	asciip += alen;
@@ -315,5 +321,5 @@
 
   case REG_BINARY:
-    return quote_buffer(datap, len, special_chars);
+    return quote_buffer(datap, len, common_special_chars);
     break;
 
@@ -366,5 +372,5 @@
 }
 
-
+/* Returns a quoted path from an nk_stack */
 char* stack2Path(void_stack* nk_stack)
 {
@@ -376,4 +382,5 @@
   char* buf; 
   char* new_buf;
+  char* name;
   void_stack_iterator* iter;
   
@@ -397,5 +404,6 @@
     buf[buf_len-buf_left-1] = '/';
     buf_left -= 1;
-    name_len = strlen(cur->keyname);
+    name = quote_string(cur->keyname, key_special_chars);
+    name_len = strlen(name);
     if(name_len+1 > buf_left)
     {
@@ -411,7 +419,8 @@
       buf_left = grow_amt + name_len + 1;
     }
-    strncpy(buf+(buf_len-buf_left-1), cur->keyname, name_len);
+    strncpy(buf+(buf_len-buf_left-1), name, name_len);
     buf_left -= name_len;
     buf[buf_len-buf_left-1] = '\0';
+    free(name);
   }
 
@@ -424,7 +433,6 @@
   uint32 size;
   uint8 tmp_buf[4];
-  char* quoted_value;
-  char* quoted_prefix;
-  char* quoted_name;
+  char* quoted_value = NULL;
+  char* quoted_name = NULL;
 
   /* Thanks Microsoft for making this process so straight-forward!!! */
@@ -460,18 +468,14 @@
    *      for that condition.
    */
-  quoted_prefix = quote_string(prefix, special_chars);
-  quoted_name = quote_string(vk->valuename, special_chars);
-  
+  quoted_name = quote_string(vk->valuename, common_special_chars);
   if(print_security)
-    printf("%s/%s,%s,%s,,,,,\n", quoted_prefix, quoted_name,
+    printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name,
 	   regfio_type_val2str(vk->type), quoted_value);
   else
-    printf("%s/%s,%s,%s,\n", quoted_prefix, quoted_name,
+    printf("%s/%s,%s,%s,\n", prefix, quoted_name,
 	   regfio_type_val2str(vk->type), quoted_value);
   
   if(quoted_value != NULL)
     free(quoted_value);
-  if(quoted_prefix != NULL)
-    free(quoted_prefix);
   if(quoted_name != NULL)
     free(quoted_name);
@@ -485,5 +489,5 @@
   for(i=0; i < nk->num_values; i++)
     if(!type_filter_enabled || (nk->values[i].type == type_filter))
-      printValue(&nk->values[i], prefix);
+      printValue(nk->values+i, prefix);
 }
 
@@ -499,10 +503,5 @@
   time_t tmp_time[1];
   struct tm* tmp_time_s = NULL;
-  char* quoted_path = NULL;
-
-  /* XXX: it would be faster to always pass a quoted string to this
-   *      function, instead of re-quoting it every call. */
-  /* XXX: check return of quote_string */
-  quoted_path = quote_string(full_path, special_chars);
+
   *tmp_time = nt_time_to_unix(&k->mtime);
   tmp_time_s = gmtime(tmp_time);
@@ -524,5 +523,5 @@
       dacl = empty_str;
 
-    printf("%s,KEY,,%s,%s,%s,%s,%s\n", quoted_path, mtime, 
+    printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime, 
 	   owner, group, sacl, dacl);
 
@@ -537,12 +536,9 @@
   }
   else
-    printf("%s,KEY,,%s\n", quoted_path, mtime);
-
-  free(quoted_path);
-}
-
-
-/* XXX: this function is awful.  Needs to be re-designed. */
-void printKeyTree(REGF_FILE* f, void_stack* nk_stack, char* prefix)
+    printf("%s,KEY,,%s\n", full_path, mtime);
+}
+
+
+void printKeyTree(REGF_FILE* f, void_stack* nk_stack, const char* prefix)
 {
   REGF_NK_REC* cur = NULL;
@@ -550,4 +546,7 @@
   char* path = NULL;
   char* val_path = NULL;
+  uint32 val_path_len = 0;
+  uint32 path_len = 0;
+  uint32 prefix_len = strlen(prefix);
   int key_type = regfio_type_str2val("KEY");
   
@@ -566,6 +565,12 @@
     }
 
-    val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
-    sprintf(val_path, "%s%s", prefix, path);
+    path_len = strlen(path);
+    val_path_len = prefix_len+path_len;
+    val_path = (char*)malloc(val_path_len+1+1);
+    if(val_path == NULL)
+      bailOut(2, "ERROR: Could not allocate val_path.\n");
+
+    strcpy(val_path, prefix);
+    strcpy(val_path+prefix_len, path);
     if(val_path[0] == '\0')
     {
@@ -573,14 +578,21 @@
       val_path[1] = '\0';
     }
-
     if(!type_filter_enabled || (key_type == type_filter))
       printKey(cur, val_path);
     if(!type_filter_enabled || (key_type != type_filter))
       printValueList(cur, val_path);
-    if(val_path != NULL)
-      free(val_path);
+    
     while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
     {
-      if((sub = regfio_fetch_subkey(f, cur)) != NULL)
+      if((sub = regfio_fetch_subkey(f, cur)) == NULL)
+      {
+	sub = void_stack_pop(nk_stack);
+	/* XXX: This is just a shallow free.  Need to write deep free
+	 * routines to replace the Samba code for this. 
+	 */ 
+	if(sub != NULL)
+	  free(sub);
+      }
+      else
       {
 	sub->subkey_index = 0;
@@ -589,25 +601,24 @@
 	if(path != NULL)
 	{
-	  val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
-	  sprintf(val_path, "%s%s", prefix, path);
+	  path_len = strlen(path);
+	  if(val_path_len < prefix_len+path_len)
+	  {
+	    val_path_len = prefix_len+path_len;
+	    val_path = (char*)realloc(val_path, val_path_len+1);
+	    if(val_path == NULL)
+	      bailOut(2, "ERROR: Could not reallocate val_path.\n");
+	  }
+	  strcpy(val_path, prefix);
+	  strcpy(val_path+prefix_len, path);
 	  if(!type_filter_enabled || (key_type == type_filter))
 	    printKey(sub, val_path);
 	  if(!type_filter_enabled || (key_type != type_filter))
 	    printValueList(sub, val_path);
-	  if(val_path != NULL)
-	    free(val_path);
 	}
       }
-      else
-      {
-	cur = void_stack_pop(nk_stack);
-	/* XXX: This is just a shallow free.  Need to write deep free
-	 * routines to replace the Samba code for this. 
-	 */ 
-	if(cur != NULL)
-	  free(cur);
-      }
-    }
-  }
+    }
+  }
+  if(val_path != NULL)
+    free(val_path);
   if(print_verbose)
     fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
@@ -627,10 +638,10 @@
   void_stack* sub_nk_stack;
   char* prefix;
-  uint32 prefix_len;
   char* cur_str = NULL;
   char* path = NULL;
+  char* name;
+  uint16 path_depth;
+  uint32 i, prefix_len;
   bool found_cur = true;
-  uint32 i;
-  uint16 path_depth;
   if(path_stack == NULL)
     return -1;
@@ -715,6 +726,8 @@
       if(prefix == NULL)
 	return -1;
+      name = quote_string(sub->keyname, key_special_chars);
       strcat(prefix, "/");
-      strcat(prefix, sub->keyname);
+      strcat(prefix, name);
+      free(name);
 
       if(print_verbose)
