Changeset 80 for trunk/src


Ignore:
Timestamp:
01/16/07 20:46:07 (18 years ago)
Author:
tim
Message:

Major API updates for regfi, and started porting reglookup to the API.

Code is non-functional, but should be soon.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/reglookup.c

    r79 r80  
    418418
    419419  ret_val = void_stack_copy_reverse(rev_ret);
    420   void_stack_destroy(rev_ret);
     420  void_stack_free(rev_ret);
    421421
    422422  return ret_val;
     
    572572
    573573
    574 void printValueList(REGF_NK_REC* nk, char* prefix)
    575 {
    576   uint32 i;
    577  
    578   for(i=0; i < nk->num_values; i++)
    579     if(!type_filter_enabled || (nk->values[i].type == type_filter))
    580       printValue(nk->values+i, prefix);
     574void printValueList(REGFI_ITERATOR i, char* prefix)
     575{
     576  REGF_VK_REC* value;
     577
     578  value = regfi_iterator_first_value(i);
     579  while(value != NULL)
     580    if(!type_filter_enabled || (value.type == type_filter))
     581      printValue(value, prefix);
    581582}
    582583
     
    715716}
    716717
    717 
    718718/*
    719  * Returns 0 if path was found.
    720  * Returns 1 if path was not found.
     719 * Returns 0 if path was not found.
     720 * Returns 1 if path was found as value.
     721 * Returns 2 if path was found as key.
    721722 * Returns less than 0 on other error.
    722723 */
    723 int retrievePath(REGF_FILE* f, void_stack* nk_stack,
    724                  void_stack* path_stack)
    725 {
    726   REGF_NK_REC* sub = NULL;
    727   REGF_NK_REC* cur = NULL;
    728   void_stack* sub_nk_stack;
    729   char* prefix;
    730   char* cur_str = NULL;
    731   char* path = NULL;
    732   char* name;
    733   uint16 path_depth;
    734   uint32 i, prefix_len;
    735   bool found_cur = true;
    736   if(path_stack == NULL)
     724int retrievePath(REGFI_ITERATOR* iter, char** path)
     725{
     726  REG_VK_REC* value;
     727  uint32 i;
     728  char* p;
     729  char* tmp_path_joined;
     730  char** tmp_path;
     731 
     732  if(path == NULL)
    737733    return -1;
    738734
    739   path_depth = void_stack_size(path_stack);
    740   if(path_depth < 1)
     735  /* One extra for any value at the end, and one more for NULL */
     736  tmp_path = (char**)malloc(sizeof(char**)*(REGF_MAX_DEPTH+1+1));
     737  if(tmp_path == NULL)
    741738    return -2;
    742739
    743   if(void_stack_size(nk_stack) < 1)
    744     return -3;
    745   cur = (REGF_NK_REC*)void_stack_cur(nk_stack);
     740  /* Strip any potential value name at end of path */
     741  for(i=0;
     742      (path[i] != NULL) && (path[i+1] != NULL)
     743        && (i < REGF_MAX_DEPTH+1+1);
     744      i++)
     745    tmp_path[i] = path[i];
     746
     747  tmp_path[i] = NULL;
    746748
    747749  if(print_verbose)
    748     fprintf(stderr, "VERBOSE: Beginning retrieval of specified path: %s\n",
     750    fprintf(stderr, "VERBOSE: Attempting to retrieve specified path: %s\n",
    749751            path_filter);
    750752
    751   while(void_stack_size(path_stack) > 1)
    752   {
    753     /* Search key records only */
    754     cur_str = (char*)void_stack_pop(path_stack);
    755 
    756     found_cur = false;
    757     while(!found_cur &&
    758           (sub = regfi_fetch_subkey(f, cur)) != NULL)
    759     {
    760       if(strcasecmp(sub->keyname, cur_str) == 0)
    761       {
    762         cur = sub;
    763         if(!void_stack_push(nk_stack, sub))
    764           bailOut(2, "ERROR: Registry maximum depth exceeded.\n");
    765 
    766         found_cur = true;
    767       }
    768     }
    769     if(print_verbose && !found_cur)
    770       fprintf(stderr, "VERBOSE: Could not find KEY '%s' in specified path.\n",
    771               cur_str);
    772 
    773     free(cur_str);
    774     if(!found_cur)
    775       return 1;
    776   }
    777 
    778   /* Last round, search value and key records */
    779   cur_str = (char*)void_stack_pop(path_stack);
    780 
    781   if(print_verbose)
    782     fprintf(stderr, "VERBOSE: Searching values for last component"
    783                     " of specified path.\n");
    784 
    785   for(i=0; (i < cur->num_values); i++)
    786   {
    787     /* XXX: Not sure when/why this can be NULL, but it's happened. */
    788     if(sub->values[i].valuename != NULL
    789        && strcasecmp(sub->values[i].valuename, cur_str) == 0)
    790     {
    791       path = stack2Path(nk_stack);
    792 
    793       if(print_verbose)
    794         fprintf(stderr, "VERBOSE: Found final path element as value.\n");
    795 
    796       if(!type_filter_enabled || (sub->values[i].type == type_filter))
    797         printValue(&sub->values[i], path);
    798       if(path != NULL)
    799         free(path);
    800 
    801       return 0;
    802     }
    803   }
    804 
    805   if(print_verbose)
    806     fprintf(stderr, "VERBOSE: Searching keys for last component"
    807                     " of specified path.\n");
    808 
    809   while((sub = regfi_fetch_subkey(f, cur)) != NULL)
    810   {
    811     if(strcasecmp(sub->keyname, cur_str) == 0)
    812     {
    813       sub_nk_stack = void_stack_new(REGF_MAX_DEPTH);
    814       if(!void_stack_push(sub_nk_stack, sub))
    815         bailOut(2, "ERROR: Registry maximum depth exceeded.\n");
    816       prefix = stack2Path(nk_stack);
    817       prefix_len = strlen(prefix);
    818       prefix = realloc(prefix, prefix_len+strlen(sub->keyname)+2);
    819       if(prefix == NULL)
    820         return -1;
    821       name = quote_string(sub->keyname, key_special_chars);
    822       strcat(prefix, "/");
    823       strcat(prefix, name);
    824       free(name);
    825 
    826       if(print_verbose)
    827         fprintf(stderr, "VERBOSE: Found final path element as key.\n");
    828 
    829       printKeyTree(f, sub_nk_stack, prefix);
    830 
    831       return 0;
    832     }
     753  if(!regfi_iterator_walk_path(iter, tmp_path))
     754  {
     755    free(tmp_path);
     756    return 0;
     757  }
     758
     759  if(regfi_iterator_find_value(iter, path[i]))
     760  {
     761    if(print_verbose)
     762      fprintf(stderr, "VERBOSE: Found final path element as value.\n");
     763
     764    value = regfi_iterator_cur_value(iter);
     765    tmp_path_joined = joinPath(tmp_path);
     766
     767    if((value == NULL) || (tmp_path_joined == NULL))
     768      bailOut(2, "ERROR: Unexpected error before printValue.\n");
     769
     770    printValue(value, tmp_path_joined);
     771
     772    free(tmp_path);
     773    free(tmp_path_joined);
     774    return 1;
     775  }
     776  else if(regfi_iterator_find_subkey(iter, path[i]))
     777  {
     778    if(print_verbose)
     779      fprintf(stderr, "VERBOSE: Found final path element as key.\n");
     780    return 2;
    833781  }
    834782
     
    836784    fprintf(stderr, "VERBOSE: Could not find last element of path.\n");
    837785
    838   return 1;
     786  return 0;
    839787}
    840788
     
    860808int main(int argc, char** argv)
    861809{
    862   void_stack* nk_stack;
    863810  void_stack* path_stack;
     811  char** path = NULL;
    864812  REGF_FILE* f;
    865813  REGF_NK_REC* root;
     814  REGFI_ITERATOR* iter;
    866815  int retr_path_ret;
    867816  uint32 argi, arge;
     
    930879  }
    931880
    932   root = regfi_rootkey(f);
    933   nk_stack = void_stack_new(REGF_MAX_DEPTH);
    934 
    935   if(void_stack_push(nk_stack, root))
    936   {
    937     if(print_header)
    938     {
    939       if(print_security)
    940         printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n");
    941       else
    942         printf("PATH,TYPE,VALUE,MTIME\n");
    943     }
    944 
    945     path_stack = path2Stack(path_filter);
    946     if(void_stack_size(path_stack) < 1)
    947       printKeyTree(f, nk_stack, "");
    948     else
    949     {
    950       retr_path_ret = retrievePath(f, nk_stack, path_stack);
    951       if(retr_path_ret == 1)
    952         fprintf(stderr, "WARNING: specified path not found.\n");
    953       else if(retr_path_ret != 0)
    954         bailOut(4, "ERROR:\n");
    955     }
     881  iter = regfi_iterator_new(f);
     882  if(iter == NULL)
     883    bailOut(3, "ERROR: Couldn't create registry iterator.\n");
     884
     885  if(path_filter_enabled && path_filter != NULL)
     886    path = splitPath(path_filter);
     887 
     888  if(path != NULL)
     889  {
     890    retr_path_ret = retrievePath(iter, path);
     891    if(retr_path_ret == 0)
     892      fprintf(stderr, "WARNING: specified path not found.\n");
     893    else if (retr_path_ret == 2)
     894      printKeyTree(iter, path_filter);
     895    else if(retr_path_ret != 0)
     896      bailOut(4, "ERROR: Unknown error occurred in retrieving path.\n");
    956897  }
    957898  else
    958     bailOut(2, "ERROR: Registry maximum depth exceeded.\n");
    959 
    960   void_stack_destroy_deep(nk_stack);
     899    printKeyTree(iter, "");
     900
     901  regfi_iterator_free(iter);
    961902  regfi_close(f);
    962903
Note: See TracChangeset for help on using the changeset viewer.