Changeset 178 for trunk/src


Ignore:
Timestamp:
03/13/10 12:56:36 (14 years ago)
Author:
tim
Message:

reworked I/O to use callback functions

fixed a bug in mtime validation and consolidated time formatting code

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/common.c

    r172 r178  
    3838
    3939
     40/* Windows is lame */
     41#ifdef O_BINARY
     42#define REGLOOKUP_OPEN_FLAGS O_RDONLY|O_BINARY
     43#else
     44#define REGLOOKUP_OPEN_FLAGS O_RDONLY
     45#endif
     46
     47
    4048void bailOut(int code, char* message)
    4149{
     
    337345  return ret_val;
    338346}
     347
     348
     349int openHive(const char* filename)
     350{
     351  int ret_val;
     352
     353  /* open an existing file */
     354  if ((ret_val = open(filename, REGLOOKUP_OPEN_FLAGS)) == -1)
     355  {
     356    fprintf(stderr, "ERROR: Failed to open hive.  Error returned: %s\n",
     357            strerror(errno));
     358    return -1;
     359  }
     360
     361  return ret_val;
     362}
     363
     364
     365void formatTime(const REGFI_NTTIME* nttime, char* output)
     366{
     367  time_t tmp_time[1];
     368  struct tm* tmp_time_s = NULL;
     369
     370  *tmp_time = regfi_nt2unix_time(nttime);
     371  tmp_time_s = gmtime(tmp_time);
     372  strftime(output,
     373           (4+1+2+1+2)+1+(2+1+2+1+2)+1,
     374              "%Y-%m-%d %H:%M:%S",
     375           tmp_time_s);
     376}
  • trunk/src/reglookup-recover.c

    r173 r178  
    4040
    4141
    42 char* getQuotedData(int fd, uint32_t offset, uint32_t length)
     42char* getQuotedData(REGFI_RAW_FILE* file_cb, uint32_t offset, uint32_t length)
    4343{
    4444  uint8_t* buf;
     
    4646  uint32_t len;
    4747
    48   if((lseek(fd, offset, SEEK_SET)) == -1)
     48  if((regfi_seek(file_cb, offset, SEEK_SET)) == -1)
    4949    return NULL;
    5050
     
    5454
    5555  len = length;
    56   if((regfi_read(fd, buf, &length) != 0) || length != len)
     56  if((regfi_read(file_cb, buf, &length) != 0) || length != len)
    5757  {
    5858    free(buf);
     
    6969void printKey(REGFI_FILE* f, REGFI_NK_REC* nk, const char* prefix)
    7070{
    71   char mtime[20];
    72   time_t tmp_time[1];
    73   struct tm* tmp_time_s = NULL;
     71  char mtime[24];
    7472  char* quoted_name = NULL;
    7573  char* quoted_raw = "";
    7674
    77   *tmp_time = regfi_nt2unix_time(&nk->mtime);
    78   tmp_time_s = gmtime(tmp_time);
    79   strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
    80 
     75  formatTime(&nk->mtime, mtime);
     76 
    8177  /* XXX: Add command line option to choose output encoding */
    8278  regfi_interpret_keyname(f, nk, REGFI_ENCODING_ASCII, true);
     
    9692
    9793  if(print_parsedraw)
    98     quoted_raw = getQuotedData(f->fd, nk->offset, nk->cell_size);
     94    quoted_raw = getQuotedData(f->cb, nk->offset, nk->cell_size);
    9995
    10096  printf("%.8X,%.8X,KEY,%s,%s,%s,%d,,,,,,,,%s\n", nk->offset, nk->cell_size,
     
    162158
    163159  if(print_parsedraw)
    164     quoted_raw = getQuotedData(f->fd, vk->offset, vk->cell_size);
     160    quoted_raw = getQuotedData(f->cb, vk->offset, vk->cell_size);
    165161
    166162  str_type = regfi_type_val2str(vk->type);
     
    195191
    196192  if(print_parsedraw)
    197     quoted_raw = getQuotedData(f->fd, sk->offset, sk->cell_size);
     193    quoted_raw = getQuotedData(f->cb, sk->offset, sk->cell_size);
    198194
    199195  if(owner == NULL)
     
    229225  bool unalloc;
    230226
    231   if(!regfi_parse_cell(f->fd, offset, NULL, 0, &cell_length, &unalloc))
     227  if(!regfi_parse_cell(f->cb, offset, NULL, 0, &cell_length, &unalloc))
    232228    return 1;
    233229
    234   quoted_buf = getQuotedData(f->fd, offset, cell_length);
     230  quoted_buf = getQuotedData(f->cb, offset, cell_length);
    235231  if(quoted_buf == NULL)
    236232    return 2;
     
    490486        max_size = regfi_calc_maxsize(file, offset);
    491487        if(max_size >= 0
    492            && regfi_parse_cell(file->fd, offset, NULL, 0,
     488           && regfi_parse_cell(file->cb, offset, NULL, 0,
    493489                               &cell_length, &unalloc)
    494490           && (cell_length & 0x00000007) == 0
     
    789785  REGFI_VK_REC* tmp_value;
    790786  uint32_t argi, arge, i, j, ret, num_unalloc_keys;
    791  
     787  int fd;
     788
    792789  /* Process command line arguments */
    793790  if(argc < 2)
     
    826823    bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n");
    827824
    828   f = regfi_open(registry_file);
    829   if(f == NULL)
     825  fd = openHive(registry_file);
     826  if(fd < 0)
    830827  {
    831828    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
    832829    bailOut(REGLOOKUP_EXIT_NOINPUT, "");
    833830  }
     831
     832  f = regfi_alloc(fd);
     833  if(f == NULL)
     834  {
     835    close(fd);
     836    bailOut(REGLOOKUP_EXIT_NOINPUT, "ERROR: Failed to create REGFI_FILE structure.\n");
     837  }
     838
    834839  if(print_verbose)
    835840    regfi_set_message_mask(f, REGFI_MSG_ERROR|REGFI_MSG_WARN|REGFI_MSG_INFO);
     
    991996  range_list_free(unalloc_sks);
    992997
     998  regfi_free(f);
     999  close(fd);
     1000
    9931001  return 0;
    9941002}
  • trunk/src/reglookup.c

    r172 r178  
    296296  char* sacl = NULL;
    297297  char* dacl = NULL;
     298  char mtime[24];
    298299  char* quoted_classname;
    299   char mtime[20];
    300   time_t tmp_time[1];
    301   struct tm* tmp_time_s = NULL;
    302300  const REGFI_SK_REC* sk;
    303301  const REGFI_NK_REC* k = regfi_iterator_cur_key(iter);
    304302  REGFI_CLASSNAME* classname;
    305303
    306   *tmp_time = regfi_nt2unix_time(&k->mtime);
    307   tmp_time_s = gmtime(tmp_time);
    308   strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
     304  formatTime(&k->mtime, mtime);
    309305
    310306  if(print_security && (sk=regfi_iterator_cur_sk(iter)))
     
    563559  char** path = NULL;
    564560  REGFI_ITERATOR* iter;
    565   int retr_path_ret;
     561  int retr_path_ret, fd;
    566562  uint32_t argi, arge;
    567563
     
    624620    bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n");
    625621
    626   f = regfi_open(registry_file);
    627   if(f == NULL)
     622  fd = openHive(registry_file);
     623  if(fd < 0)
    628624  {
    629625    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
    630626    bailOut(REGLOOKUP_EXIT_NOINPUT, "");
     627  }
     628
     629  f = regfi_alloc(fd);
     630  if(f == NULL)
     631  {
     632    close(fd);
     633    bailOut(REGLOOKUP_EXIT_NOINPUT, "ERROR: Failed to create REGFI_FILE structure.\n");
    631634  }
    632635
     
    675678
    676679  regfi_iterator_free(iter);
    677   regfi_close(f);
     680  regfi_free(f);
     681  close(fd);
    678682
    679683  return 0;
Note: See TracChangeset for help on using the changeset viewer.