Changeset 33


Ignore:
Timestamp:
07/17/05 15:03:02 (19 years ago)
Author:
tim
Message:

Fixed some bugs.

rewrote testing code to allow for a path filter expression.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/void_stack.h

    r31 r33  
    4444void_stack_iterator* void_stack_iterator_new(void_stack* stack);
    4545void void_stack_iterator_destroy(void_stack_iterator* iter);
    46 void* void_stack_iterator_next(void_stack_iterator* iter);
     46const void* void_stack_iterator_next(void_stack_iterator* iter);
  • trunk/lib/regfio.c

    r32 r33  
    254254/*******************************************************************
    255255 *******************************************************************/
    256 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
     256static bool prs_nk_rec( const char *desc, prs_struct *ps,
     257                        int depth, REGF_NK_REC *nk )
    257258{
    258259  uint16 class_length, name_length;
     
    266267  depth++;
    267268       
    268   /* back up and get the data_size */
    269        
     269  /* back up and get the data_size */   
    270270  if ( !prs_set_offset( ps, ps->data_offset-sizeof(uint32)) )
    271271    return false;
     
    274274    return false;
    275275       
    276   if ( !prs_uint8s( true, "header", ps, depth, nk->header, sizeof( nk->header )) )
     276  if (!prs_uint8s(true, "header", ps, depth, nk->header, sizeof(nk->header)))
    277277    return false;
    278278               
     
    307307    return false;
    308308
    309   if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
    310     return false;
    311   if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
    312     return false;
     309  if (!prs_uint32("max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
     310    return false;
     311  if ( !prs_uint32( "max_bytes_subkeyclassname", ps,
     312                    depth, &nk->max_bytes_subkeyclassname))
     313  { return false; }
    313314  if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
    314315    return false;
     
    325326    return false;       
    326327               
    327   if ( class_length ) {
     328  if ( class_length )
     329  {
    328330    ;;
    329331  }
    330332       
    331   if ( name_length ) {
    332     if ( ps->io ) {
    333       if ( !(nk->keyname = (char*)zcalloc(sizeof(char), name_length+1 )) )
     333  if ( name_length )
     334  {
     335    if(ps->io && !(nk->keyname = (char*)zcalloc(sizeof(char), name_length+1)))
    334336        return false;
    335     }
    336337
    337338    if ( !prs_uint8s( true, "name", ps, depth, nk->keyname, name_length) )
     
    344345  end_off = ps->data_offset;
    345346
    346   /* data_size must be divisible by 8 and large enough to hold the original record */
     347  /* data_size must be divisible by 8 and large enough to hold
     348     the original record */
    347349
    348350  data_size = ((start_off - end_off) & 0xfffffff8 );
    349   if ( data_size > nk->rec_size )
    350     /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));*/
    351 
    352     if ( !ps->io )
    353       nk->hbin->dirty = true;
    354 
     351  /*if ( data_size > nk->rec_size )
     352      DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));*/
     353
     354  if ( !ps->io )
     355    nk->hbin->dirty = true;
     356 
     357  nk->subkey_index = 0;
    355358  return true;
    356359}
     
    627630
    628631  data_size = ((start_off - end_off) & 0xfffffff8 );
    629   if ( data_size > lf->rec_size )
     632  /*  if ( data_size > lf->rec_size )*/
    630633    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));*/
    631634
    632     if ( !hbin->ps.io )
    633       hbin->dirty = true;
     635  if ( !hbin->ps.io )
     636    hbin->dirty = true;
    634637
    635638  return true;
     
    681684
    682685  data_size = ((start_off - end_off) & 0xfffffff8 );
    683   if ( data_size > sk->rec_size )
     686  /*  if ( data_size > sk->rec_size )*/
    684687    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));*/
    685688
    686     if ( !hbin->ps.io )
    687       hbin->dirty = true;
     689  if ( !hbin->ps.io )
     690    hbin->dirty = true;
    688691
    689692  return true;
     
    937940
    938941  /* get the initial nk record */
    939        
    940   if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
     942  if (!prs_nk_rec("nk_rec", &hbin->ps, depth, nk))
    941943    return false;
    942944
    943945  /* fill in values */
    944        
    945946  if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) )
    946947  {
     
    12241225
    12251226
     1227/* XXX: An interator struct should be used instead, and this function
     1228 *   should operate on it, so the state of iteration isn't stored in the
     1229 * REGF_NK_REC struct itself.
     1230 */
    12261231/*******************************************************************
    12271232 This acts as an interator over the subkeys defined for a given
  • trunk/lib/void_stack.c

    r31 r33  
    5656    free(stack->elements[i]);
    5757  free(stack);
     58}
     59
     60
     61unsigned short void_stack_size(void_stack* stack)
     62{
     63  return stack->top;
    5864}
    5965
     
    121127
    122128
    123 void* void_stack_iterator_next(void_stack_iterator* iter)
     129const void* void_stack_iterator_next(void_stack_iterator* iter)
    124130{
    125131  if(iter->cur < iter->stack->top)
  • trunk/test/lib-regfio.c

    r32 r33  
    2424#include <stdio.h>
    2525#include <string.h>
     26#include <strings.h>
    2627#include "../include/regfio.h"
    2728#include "../include/void_stack.h"
    2829
    2930
    30 char* getStackPath(void_stack* nk_stack)
    31 {
    32   REGF_NK_REC* cur;
     31void_stack* path2Stack(const char* s)
     32{
     33  void_stack* ret_val = void_stack_new(1024);
     34  char* cur = strdup(s);
     35  char* next = NULL;
     36 
     37  while((next = strrchr(cur, '/')) != NULL)
     38  {
     39    next[0] = '\0';
     40    if(strlen(next+1) > 0)
     41      void_stack_push(ret_val, next+1);
     42  }
     43  if(strlen(cur) > 0)
     44    void_stack_push(ret_val, cur);
     45
     46  return ret_val;
     47}
     48
     49
     50char* stack2Path(void_stack* nk_stack)
     51{
     52  const REGF_NK_REC* cur;
    3353  unsigned int buf_left = 127;
    3454  unsigned int buf_len = buf_left+1;
     
    5171  }
    5272
     73  /* skip root element */
     74  cur = void_stack_iterator_next(iter);
     75
    5376  while((cur = void_stack_iterator_next(iter)) != NULL)
    5477  {
     78    buf[buf_len-buf_left-1] = '/';
     79    buf_left -= 1;
    5580    name_len = strlen(cur->keyname);
    5681    if(name_len+1 > buf_left)
    5782    {
    58       grow_amt = (unsigned int)(buf_len/3);
     83      grow_amt = (unsigned int)(buf_len/2);
    5984      buf_len += name_len+1+grow_amt-buf_left;
    6085      if((new_buf = realloc(buf, buf_len)) == NULL)
     
    6994    strncpy(buf+(buf_len-buf_left-1), cur->keyname, name_len);
    7095    buf_left -= name_len;
    71     buf[buf_len-buf_left-1] = '/';
    72     buf_left -= 1;
    7396    buf[buf_len-buf_left-1] = '\0';
    7497  }
    7598
    76   /* Cut trailing slash */ 
    77   if(buf[buf_len-buf_left-2] == '/')
    78     buf[buf_len-buf_left-2] = '\0';
    79 
    8099  return buf;
    81100}
    82101
    83102
    84 void printKeyList(REGF_NK_REC* nk, char* prefix)
     103void printValue(REGF_VK_REC* vk, char* prefix)
     104{
     105  const char* str_type;
     106 
     107  str_type = type_val2str(vk->type);
     108  printf("%s/%s:%s=\n", prefix, vk->valuename, str_type);
     109}
     110
     111
     112void printValueList(REGF_NK_REC* nk, char* prefix)
    85113{
    86114  unsigned int i;
    87   const char* str_type;
    88115 
    89116  for(i=0; i < nk->num_values; i++)
    90   {
    91     str_type = type_val2str(nk->values[i].type);
    92     printf("%s/%s:%s=\n", prefix, nk->values[i].valuename, str_type);
    93   }
    94 }
    95 
    96 
    97 void printKeyTree(REGF_FILE* f, void_stack* nk_stack)
     117    printValue(&nk->values[i], prefix);
     118}
     119
     120/* XXX: this function is god-awful.  Needs to be re-designed. */
     121void printKeyTree(REGF_FILE* f, void_stack* nk_stack, char* prefix)
    98122{
    99123  REGF_NK_REC* cur;
    100124  REGF_NK_REC* sub;
    101125  char* path;
     126  char* val_path;
    102127
    103128  if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
    104129  {
    105     printf("%s:KEY\n", cur->keyname);
     130    cur->subkey_index = 0;
     131    path = stack2Path(nk_stack);
     132   
     133    if(strlen(path) > 0)
     134      printf("%s%s:KEY\n", prefix, path);
     135    printValueList(cur, path);
    106136    while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
    107137    {
    108138      if((sub = regfio_fetch_subkey(f, cur)) != NULL)
    109139      {
     140        sub->subkey_index = 0;
    110141        void_stack_push(nk_stack, sub);
    111         path = getStackPath(nk_stack);
     142        path = stack2Path(nk_stack);
    112143        if(path != NULL)
    113144        {
    114           printKeyList(cur, path);
    115           printf("%s:KEY\n", path);
     145          val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
     146          sprintf(val_path, "%s%s", prefix, path);
     147          printf("%s:KEY\n", val_path);
     148          printValueList(sub, val_path);
     149          free(val_path);
    116150          free(path);
    117151        }
     
    123157         * routines to replace the Samba code for this.
    124158         */
    125         if(cur != NULL)
    126           free(cur);
    127       }
    128     }
    129   }
     159        /*      if(cur != NULL)
     160          free(cur);*/
     161      }
     162    }
     163  }
     164}
     165
     166
     167/*
     168 * Returns 0 if path was found.
     169 * Returns 1 if path was not found.
     170 * Returns less than 0 on other error.
     171 */
     172int retrievePath(REGF_FILE* f, void_stack* nk_stack,
     173                 void_stack* path_stack)
     174{
     175  REGF_NK_REC* sub;
     176  REGF_NK_REC* cur;
     177  void_stack* sub_nk_stack;
     178  char* prefix;
     179  char* cur_str = NULL;
     180  bool found_cur = true;
     181  unsigned int i;
     182  unsigned short path_depth;
     183  if(path_stack == NULL)
     184    return -1;
     185
     186  path_depth = void_stack_size(path_stack);
     187  if(path_depth < 1)
     188    return -2;
     189
     190  if(void_stack_size(nk_stack) < 1)
     191    return -3;
     192  cur = (REGF_NK_REC*)void_stack_cur(nk_stack);
     193
     194  while(void_stack_size(path_stack) > 1)
     195  {
     196    /* Search key records only */
     197    cur_str = (char*)void_stack_pop(path_stack);
     198
     199    found_cur = false;
     200    while(!found_cur &&
     201          (sub = regfio_fetch_subkey(f, cur)) != NULL)
     202    {
     203      if(strcasecmp(sub->keyname, cur_str) == 0)
     204      {
     205        cur = sub;
     206        void_stack_push(nk_stack, sub);
     207        found_cur = true;
     208      }
     209    }
     210
     211    if(!found_cur)
     212      return 0;
     213  }
     214
     215  /* Last round, search value and key records */
     216  cur_str = (char*)void_stack_pop(path_stack);
     217
     218  for(i=0; (i < cur->num_values); i++)
     219  {
     220    if(strcasecmp(sub->values[i].valuename, cur_str) == 0)
     221    {
     222      printValue(&sub->values[i], stack2Path(nk_stack));
     223      return 0;
     224    }
     225  }
     226
     227  while((sub = regfio_fetch_subkey(f, cur)) != NULL)
     228  {
     229    if(strcasecmp(sub->keyname, cur_str) == 0)
     230    {
     231      sub_nk_stack = void_stack_new(1024);
     232      void_stack_push(sub_nk_stack, sub);
     233      void_stack_push(nk_stack, sub);
     234      prefix = stack2Path(nk_stack);
     235      printKeyTree(f, sub_nk_stack, prefix);
     236      return 0;
     237    }
     238  }
     239
     240  return 1;
    130241}
    131242
     
    134245{
    135246  void_stack* nk_stack;
     247  void_stack* path_stack;
    136248  REGF_FILE* f;
    137249  REGF_NK_REC* root;
     250  int retr_path_ret;
     251  char* path = "/ControlSet002/Services/Eventlog/";
    138252
    139253  if(argc < 2)
     
    148262  nk_stack = void_stack_new(1024);
    149263  if(void_stack_push(nk_stack, root))
    150     printKeyTree(f, nk_stack);
     264  {
     265    path_stack = path2Stack(path);
     266    if(void_stack_size(path_stack) < 1)
     267      printKeyTree(f, nk_stack, "");
     268    else
     269    {
     270      retr_path_ret = retrievePath(f, nk_stack,
     271                                   path_stack);
     272      if(retr_path_ret == 1)
     273        printf("WARNING: specified path not found.\n");
     274      else if(retr_path_ret != 0)
     275        printf("ERROR\n");
     276    }
     277  }
    151278  void_stack_destroy(nk_stack);
    152279
    153280  regfio_close(f);
    154 /*
    155 REGF_NK_REC*  regfio_rootkey( REGF_FILE *file );
    156 REGF_NK_REC*  regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk );
    157 */
    158281
    159282  return 0;
Note: See TracChangeset for help on using the changeset viewer.