Changeset 149


Ignore:
Timestamp:
02/23/09 00:23:45 (16 years ago)
Author:
tim
Message:

minor efficiency improvement for path construction in reglookup-recover

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/regfi.h

    r147 r149  
    8989
    9090/* XXX: This is totally arbitrary right now. */
    91 #define REGFI_MAX_SUBKEY_DEPTH     255   
     91#define REGFI_MAX_SUBKEY_DEPTH     255
    9292
    9393/* Header sizes and magic number lengths for various records */
  • trunk/src/reglookup-recover.c

    r148 r149  
    252252 */
    253253/* XXX: This is not terribly efficient, as it may reparse many keys
    254  *      repeatedly.  Should try to add caching.  Also, piecing the path
    255  *      together is slow and redundant.
     254 *      repeatedly.  Should try to add caching.
    256255 */
    257256char* getParentPath(REGFI_FILE* f, REGFI_NK_REC* nk)
     
    261260  REGFI_NK_REC* cur_ancestor;
    262261  char* ret_val;
    263   char* path_element;
    264   char* tmp_str;
    265   uint32 virt_offset, i, stack_size, ret_val_size, ret_val_left, element_size;
     262  uint32 virt_offset, i, stack_size, ret_val_size, ret_val_used;
    266263  uint32 max_length;
    267 
     264  /* A little hack so we don't have to walk a quoted string twice. */
     265  struct name_holder
     266  {
     267    char* quoted_name;
     268    uint32_t length;
     269  };
     270  struct name_holder* path_element;
     271
     272 
    268273  /* The path_stack size limit should guarantee that we don't recurse forever. */
    269274  virt_offset = nk->parent_off;
     275  ret_val_size = 1; /* NUL */
    270276  while(virt_offset != REGFI_OFFSET_NONE)
    271277  { 
     
    291297          virt_offset = cur_ancestor->parent_off;
    292298       
    293         path_element = quote_string(cur_ancestor->keyname, key_special_chars);
    294         if(path_element == NULL || !void_stack_push(path_stack, path_element))
     299        path_element = (struct name_holder*)malloc(sizeof(struct name_holder));
     300        if(path_element != NULL)
     301          path_element->quoted_name = quote_string(cur_ancestor->keyname,
     302                                                   key_special_chars);
     303         
     304        if(path_element == NULL || path_element->quoted_name == NULL
     305           || !void_stack_push(path_stack, path_element))
    295306        {
    296307          free(cur_ancestor->keyname);
     
    300311        }
    301312
     313        /* Path element and preceeding delimiter
     314         * Note that this integer can't overflow since key name lengths are
     315         * 16 bits and the max depth is 512.
     316         */
     317        path_element->length = strlen(path_element->quoted_name);
     318        ret_val_size += path_element->length + 1;
     319
    302320        regfi_key_free(cur_ancestor);
    303321      }
     
    306324 
    307325  stack_size = void_stack_size(path_stack);
    308   ret_val_size = 16*stack_size;
    309   if(ret_val_size == 0)
    310     ret_val_size = 1;
    311   ret_val_left = ret_val_size;
     326  ret_val_used = 0;
    312327  ret_val = malloc(ret_val_size);
    313328  if(ret_val == NULL)
     
    321336  {
    322337    path_element = void_stack_pop(path_stack);
    323     element_size = strlen(path_element);
    324     if(ret_val_left < element_size+2)
    325     {
    326       ret_val_size += element_size+16;
    327       ret_val_left += element_size+16;
    328       tmp_str = (char*)realloc(ret_val, ret_val_size);
    329       if(tmp_str == NULL)
    330       {
    331         free(ret_val);
    332         void_stack_free_deep(path_stack);
    333         return NULL;
    334       }
    335       ret_val = tmp_str;
    336     }
    337 
    338     ret_val_left -= snprintf(ret_val+ret_val_size-ret_val_left,ret_val_left, "/%s", path_element);
     338    snprintf(ret_val+ret_val_used, ret_val_size-ret_val_used,
     339             "/%s", path_element->quoted_name);
     340    ret_val_used += path_element->length + 1;
     341    free(path_element->quoted_name);
    339342    free(path_element);
    340343  }
Note: See TracChangeset for help on using the changeset viewer.