source: trunk/lib/regfi.c @ 261

Last change on this file since 261 was 261, checked in by tim, 13 years ago

readded windows file descriptor hack
copyright notices

  • Property svn:keywords set to Id
File size: 106.4 KB
RevLine 
[30]1/*
[261]2 * Copyright (C) 2005-2011 Timothy D. Morgan
[30]3 * Copyright (C) 2005 Gerald (Jerry) Carter
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
[111]7 * the Free Software Foundation; version 3 of the License.
[30]8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
[161]16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
[30]17 *
18 * $Id: regfi.c 261 2011-06-17 00:55:49Z tim $
19 */
20
[169]21/**
22 * @file
23 *
24 * Windows NT (and later) read-only registry library
25 *
26 * See @ref regfi.h for more information.
27 *
28 * Branched from Samba project Subversion repository, version #7470:
29 *   http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/registry/regfio.c?rev=7470&view=auto
30 *
31 * Since then, it has been heavily rewritten, simplified, and improved.
32 */
[168]33
[147]34#include "regfi.h"
[30]35
[233]36/* Library version can be overridden at build time */
37#ifndef REGFI_VERSION
38#define REGFI_VERSION "trunk"
39#endif
[30]40
[233]41
[32]42/* Registry types mapping */
[78]43const unsigned int regfi_num_reg_types = 12;
44static const char* regfi_type_names[] =
[65]45  {"NONE", "SZ", "EXPAND_SZ", "BINARY", "DWORD", "DWORD_BE", "LINK",
[72]46   "MULTI_SZ", "RSRC_LIST", "RSRC_DESC", "RSRC_REQ_LIST", "QWORD"};
[30]47
[161]48const char* regfi_encoding_names[] =
49  {"US-ASCII//TRANSLIT", "UTF-8//TRANSLIT", "UTF-16LE//TRANSLIT"};
[32]50
[135]51
[185]52/* Ensures regfi_init runs only once */
53static pthread_once_t regfi_init_once = PTHREAD_ONCE_INIT;
[182]54
[185]55
[233]56/******************************************************************************
57 ******************************************************************************/
58const char* regfi_version()
59{
60  return REGFI_VERSION;
61}
[185]62
[233]63
[135]64/******************************************************************************
65 ******************************************************************************/
[185]66void regfi_log_free(void* ptr)
[135]67{
[185]68  REGFI_LOG* log_info = (REGFI_LOG*)ptr;
69 
70  if(log_info->messages != NULL)
71    free(log_info->messages);
72
73  talloc_free(log_info);
74}
75
76
77/******************************************************************************
78 ******************************************************************************/
79void regfi_init()
80{
81  int err;
82  if((err = pthread_key_create(&regfi_log_key, regfi_log_free)) != 0)
83    fprintf(stderr, "ERROR: key_create: %s\n", strerror(err));
84  errno = err;
85}
86
87
88/******************************************************************************
89 ******************************************************************************/
90REGFI_LOG* regfi_log_new()
91{
92  int err;
[182]93  REGFI_LOG* log_info = talloc(NULL, REGFI_LOG);
94  if(log_info == NULL)
[185]95    return NULL;
[182]96
[185]97  log_info->msg_mask = REGFI_DEFAULT_LOG_MASK;
[182]98  log_info->messages = NULL;
99
[185]100  pthread_once(&regfi_init_once, regfi_init);
[182]101
[185]102  if((err = pthread_setspecific(regfi_log_key, log_info)) != 0)
[182]103  {
[185]104    fprintf(stderr, "ERROR: setspecific: %s\n", strerror(err));
[182]105    goto fail;
106  }
107
[185]108  return log_info;
[182]109
110 fail:
111  talloc_free(log_info);
[185]112  errno = err;
113  return NULL;
[182]114}
115
116
117/******************************************************************************
118 ******************************************************************************/
119void regfi_log_add(uint16_t msg_type, const char* fmt, ...)
120{
121  /* XXX: Switch internal storage over to a linked list or stack.
122   *      Then add a regfi_log_get function that returns the list in some
123   *      convenient, user-friendly data structure.  regfi_log_get_str should
124   *      stick around and will simply smush the list into a big string when
125   *      it's called, rather than having messages smushed when they're first
126   *      written to the log.
[135]127   */
[168]128  uint32_t buf_size, buf_used;
[136]129  char* new_msg;
[182]130  REGFI_LOG* log_info;
[136]131  va_list args;
[135]132
[185]133  log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
134  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
[182]135    return;
136
[185]137  if((log_info->msg_mask & msg_type) == 0)
138    return;
139
[182]140  if(log_info->messages == NULL)
141    buf_used = 0;
142  else
143    buf_used = strlen(log_info->messages);
144 
145  buf_size = buf_used+strlen(fmt)+160;
146  new_msg = realloc(log_info->messages, buf_size);
147  if(new_msg == NULL)
148    /* XXX: should we report this? */
149    return;
150 
151  switch (msg_type)
[138]152  {
[182]153  case REGFI_LOG_INFO:
154    strcpy(new_msg+buf_used, "INFO: ");
155    buf_used += 6;
156    break;
157  case REGFI_LOG_WARN:
158    strcpy(new_msg+buf_used, "WARN: ");
159    buf_used += 6;
160    break;
161  case REGFI_LOG_ERROR:
162    strcpy(new_msg+buf_used, "ERROR: ");
163    buf_used += 7;
164    break;
[138]165  }
[182]166 
167  va_start(args, fmt);
168  vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args);
169  va_end(args);
170  strncat(new_msg, "\n", buf_size-1);
171 
172  log_info->messages = new_msg;
[135]173}
174
175
176/******************************************************************************
177 ******************************************************************************/
[182]178char* regfi_log_get_str()
[135]179{
[182]180  char* ret_val;
[185]181  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
182  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
[182]183    return NULL;
[185]184 
[182]185  ret_val = log_info->messages;
186  log_info->messages = NULL;
187
[135]188  return ret_val;
189}
190
191
[182]192/******************************************************************************
193 ******************************************************************************/
[185]194bool regfi_log_set_mask(uint16_t msg_mask)
[138]195{
[185]196  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
197  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
198  {
199      return false;
200  }
[182]201
202  log_info->msg_mask = msg_mask;
[185]203  return true;
[138]204}
205
206
[161]207/******************************************************************************
208 * Returns NULL for an invalid e
209 *****************************************************************************/
210static const char* regfi_encoding_int2str(REGFI_ENCODING e)
211{
212  if(e < REGFI_NUM_ENCODINGS)
213    return regfi_encoding_names[e];
214
215  return NULL;
216}
217
218
219/******************************************************************************
220 * Returns NULL for an invalid val
221 *****************************************************************************/
[78]222const char* regfi_type_val2str(unsigned int val)
[32]223{
[61]224  if(val == REG_KEY)
225    return "KEY";
226 
[78]227  if(val >= regfi_num_reg_types)
[61]228    return NULL;
229 
[78]230  return regfi_type_names[val];
[32]231}
232
233
[161]234/******************************************************************************
235 * Returns -1 on error
236 *****************************************************************************/
[78]237int regfi_type_str2val(const char* str)
[32]238{
239  int i;
240
[61]241  if(strcmp("KEY", str) == 0)
242    return REG_KEY;
[32]243
[78]244  for(i=0; i < regfi_num_reg_types; i++)
245    if (strcmp(regfi_type_names[i], str) == 0) 
[61]246      return i;
247
248  if(strcmp("DWORD_LE", str) == 0)
249    return REG_DWORD_LE;
250
251  return -1;
[32]252}
253
254
[135]255/* Security descriptor formatting functions  */
[53]256
[168]257const char* regfi_ace_type2str(uint8_t type)
[53]258{
259  static const char* map[7] 
260    = {"ALLOW", "DENY", "AUDIT", "ALARM", 
261       "ALLOW CPD", "OBJ ALLOW", "OBJ DENY"};
262  if(type < 7)
263    return map[type];
264  else
265    /* XXX: would be nice to return the unknown integer value. 
266     *      However, as it is a const string, it can't be free()ed later on,
267     *      so that would need to change.
268     */
269    return "UNKNOWN";
270}
271
272
[76]273/* XXX: need a better reference on the meaning of each flag. */
274/* For more info, see:
275 *   http://msdn2.microsoft.com/en-us/library/aa772242.aspx
276 */
[168]277char* regfi_ace_flags2str(uint8_t flags)
[53]278{
[76]279  static const char* flag_map[32] = 
[87]280    { "OI", /* Object Inherit */
281      "CI", /* Container Inherit */
282      "NP", /* Non-Propagate */
283      "IO", /* Inherit Only */
284      "IA", /* Inherited ACE */
[76]285      NULL,
286      NULL,
287      NULL,
288    };
[53]289
[76]290  char* ret_val = malloc(35*sizeof(char));
291  char* fo = ret_val;
[168]292  uint32_t i;
293  uint8_t f;
[76]294
295  if(ret_val == NULL)
[53]296    return NULL;
297
[76]298  fo[0] = '\0';
[53]299  if (!flags)
[76]300    return ret_val;
[53]301
[76]302  for(i=0; i < 8; i++)
303  {
304    f = (1<<i);
305    if((flags & f) && (flag_map[i] != NULL))
306    {
307      strcpy(fo, flag_map[i]);
308      fo += strlen(flag_map[i]);
309      *(fo++) = ' ';
310      flags ^= f;
311    }
[53]312  }
[76]313 
314  /* Any remaining unknown flags are added at the end in hex. */
315  if(flags != 0)
316    sprintf(fo, "0x%.2X ", flags);
317
318  /* Chop off the last space if we've written anything to ret_val */
319  if(fo != ret_val)
320    fo[-1] = '\0';
321
322  return ret_val;
[53]323}
324
325
[168]326char* regfi_ace_perms2str(uint32_t perms)
[53]327{
[168]328  uint32_t i, p;
[76]329  /* This is more than is needed by a fair margin. */
330  char* ret_val = malloc(350*sizeof(char));
331  char* r = ret_val;
332
333  /* Each represents one of 32 permissions bits.  NULL is for undefined/reserved bits.
334   * For more information, see:
335   *   http://msdn2.microsoft.com/en-gb/library/aa374892.aspx
336   *   http://msdn2.microsoft.com/en-gb/library/ms724878.aspx
337   */
338  static const char* perm_map[32] = 
339    {/* object-specific permissions (registry keys, in this case) */
340      "QRY_VAL",       /* KEY_QUERY_VALUE */
341      "SET_VAL",       /* KEY_SET_VALUE */
342      "CREATE_KEY",    /* KEY_CREATE_SUB_KEY */
343      "ENUM_KEYS",     /* KEY_ENUMERATE_SUB_KEYS */
344      "NOTIFY",        /* KEY_NOTIFY */
345      "CREATE_LNK",    /* KEY_CREATE_LINK - Reserved for system use. */
346      NULL,
347      NULL,
348      "WOW64_64",      /* KEY_WOW64_64KEY */
349      "WOW64_32",      /* KEY_WOW64_32KEY */
350      NULL,
351      NULL,
352      NULL,
353      NULL,
354      NULL,
355      NULL,
356      /* standard access rights */
357      "DELETE",        /* DELETE */
358      "R_CONT",        /* READ_CONTROL */
359      "W_DAC",         /* WRITE_DAC */
360      "W_OWNER",       /* WRITE_OWNER */
361      "SYNC",          /* SYNCHRONIZE - Shouldn't be set in registries */
362      NULL,
363      NULL,
364      NULL,
365      /* other generic */
366      "SYS_SEC",       /* ACCESS_SYSTEM_SECURITY */
367      "MAX_ALLWD",     /* MAXIMUM_ALLOWED */
368      NULL,
369      NULL,
370      "GEN_A",         /* GENERIC_ALL */
371      "GEN_X",         /* GENERIC_EXECUTE */
372      "GEN_W",         /* GENERIC_WRITE */
373      "GEN_R",         /* GENERIC_READ */
374    };
375
376
[53]377  if(ret_val == NULL)
378    return NULL;
379
[76]380  r[0] = '\0';
381  for(i=0; i < 32; i++)
382  {
383    p = (1<<i);
384    if((perms & p) && (perm_map[i] != NULL))
385    {
386      strcpy(r, perm_map[i]);
387      r += strlen(perm_map[i]);
388      *(r++) = ' ';
389      perms ^= p;
390    }
391  }
392 
393  /* Any remaining unknown permission bits are added at the end in hex. */
394  if(perms != 0)
395    sprintf(r, "0x%.8X ", perms);
[53]396
[76]397  /* Chop off the last space if we've written anything to ret_val */
398  if(r != ret_val)
399    r[-1] = '\0';
400
[53]401  return ret_val;
402}
403
404
[134]405char* regfi_get_acl(WINSEC_ACL* acl)
[53]406{
[168]407  uint32_t i, extra, size = 0;
[53]408  const char* type_str;
409  char* flags_str;
410  char* perms_str;
411  char* sid_str;
[61]412  char* ace_delim = "";
[53]413  char* ret_val = NULL;
[61]414  char* tmp_val = NULL;
415  bool failed = false;
[53]416  char field_delim = ':';
417
[61]418  for (i = 0; i < acl->num_aces && !failed; i++)
[53]419  {
[253]420    sid_str = winsec_sid2str(acl->aces[i]->trustee);
[134]421    type_str = regfi_ace_type2str(acl->aces[i]->type);
422    perms_str = regfi_ace_perms2str(acl->aces[i]->access_mask);
423    flags_str = regfi_ace_flags2str(acl->aces[i]->flags);
[53]424   
[61]425    if(flags_str != NULL && perms_str != NULL 
426       && type_str != NULL && sid_str != NULL)
427    {
428      /* XXX: this is slow */
429      extra = strlen(sid_str) + strlen(type_str) 
[136]430        + strlen(perms_str) + strlen(flags_str) + 5;
[61]431      tmp_val = realloc(ret_val, size+extra);
[53]432
[61]433      if(tmp_val == NULL)
434      {
435        free(ret_val);
[136]436        ret_val = NULL;
[61]437        failed = true;
438      }
439      else
440      {
441        ret_val = tmp_val;
[148]442        size += sprintf(ret_val+size, "%s%s%c%s%c%s%c%s",
443                        ace_delim,sid_str,
444                        field_delim,type_str,
445                        field_delim,perms_str,
446                        field_delim,flags_str);
[61]447        ace_delim = "|";
448      }
449    }
450    else
451      failed = true;
452
453    if(sid_str != NULL)
454      free(sid_str);
455    if(sid_str != NULL)
456      free(perms_str);
457    if(sid_str != NULL)
458      free(flags_str);
[53]459  }
460
461  return ret_val;
462}
463
464
[134]465char* regfi_get_sacl(WINSEC_DESC *sec_desc)
[53]466{
467  if (sec_desc->sacl)
[78]468    return regfi_get_acl(sec_desc->sacl);
[53]469  else
470    return NULL;
471}
472
473
[134]474char* regfi_get_dacl(WINSEC_DESC *sec_desc)
[53]475{
476  if (sec_desc->dacl)
[78]477    return regfi_get_acl(sec_desc->dacl);
[53]478  else
479    return NULL;
480}
481
482
[134]483char* regfi_get_owner(WINSEC_DESC *sec_desc)
[53]484{
[253]485  return winsec_sid2str(sec_desc->owner_sid);
[53]486}
487
488
[134]489char* regfi_get_group(WINSEC_DESC *sec_desc)
[53]490{
[253]491  return winsec_sid2str(sec_desc->grp_sid);
[53]492}
493
494
[180]495bool regfi_read_lock(REGFI_FILE* file, pthread_rwlock_t* lock, const char* context)
496{
497  int lock_ret = pthread_rwlock_rdlock(lock);
498  if(lock_ret != 0)
499  {
[182]500    regfi_log_add(REGFI_LOG_ERROR, "Error obtaining read lock in"
[180]501                      "%s due to: %s\n", context, strerror(lock_ret));
502    return false;
503  }
504
505  return true;
506}
507
508
509bool regfi_write_lock(REGFI_FILE* file, pthread_rwlock_t* lock, const char* context)
510{
511  int lock_ret = pthread_rwlock_wrlock(lock);
512  if(lock_ret != 0)
513  {
[182]514    regfi_log_add(REGFI_LOG_ERROR, "Error obtaining write lock in"
[180]515                      "%s due to: %s\n", context, strerror(lock_ret));
516    return false;
517  }
518
519  return true;
520}
521
522
523bool regfi_rw_unlock(REGFI_FILE* file, pthread_rwlock_t* lock, const char* context)
524{
525  int lock_ret = pthread_rwlock_unlock(lock);
526  if(lock_ret != 0)
527  {
[182]528    regfi_log_add(REGFI_LOG_ERROR, "Error releasing lock in"
[180]529                      "%s due to: %s\n", context, strerror(lock_ret));
530    return false;
531  }
532
533  return true;
534}
535
536
537bool regfi_lock(REGFI_FILE* file, pthread_mutex_t* lock, const char* context)
538{
539  int lock_ret = pthread_mutex_lock(lock);
540  if(lock_ret != 0)
541  {
[182]542    regfi_log_add(REGFI_LOG_ERROR, "Error obtaining mutex lock in"
[180]543                      "%s due to: %s\n", context, strerror(lock_ret));
544    return false;
545  }
546
547  return true;
548}
549
550
551bool regfi_unlock(REGFI_FILE* file, pthread_mutex_t* lock, const char* context)
552{
553  int lock_ret = pthread_mutex_unlock(lock);
554  if(lock_ret != 0)
555  {
[182]556    regfi_log_add(REGFI_LOG_ERROR, "Error releasing mutex lock in"
[180]557                      "%s due to: %s\n", context, strerror(lock_ret));
558    return false;
559  }
560
561  return true;
562}
563
564
[226]565int64_t regfi_raw_seek(REGFI_RAW_FILE* self, uint64_t offset, int whence)
[178]566{
[226]567  if(sizeof(off_t) == 4 && offset > 2147483647)
568  {
569    errno = EOVERFLOW;
570    return -1;
571  }
[178]572  return lseek(*(int*)self->state, offset, whence);
573}
574
575ssize_t regfi_raw_read(REGFI_RAW_FILE* self, void* buf, size_t count)
576{
577  return read(*(int*)self->state, buf, count);
578}
579
580
[101]581/*****************************************************************************
[178]582 * Convenience function to wrap up the ugly callback stuff
583 *****************************************************************************/
[226]584uint64_t regfi_seek(REGFI_RAW_FILE* file_cb, uint64_t offset, int whence)
[178]585{
586  return file_cb->seek(file_cb, offset, whence);
587}
588
589
590/*****************************************************************************
[101]591 * This function is just like read(2), except that it continues to
592 * re-try reading from the file descriptor if EINTR or EAGAIN is received. 
[178]593 * regfi_read will attempt to read length bytes from the file and write them to
594 * buf.
[101]595 *
596 * On success, 0 is returned.  Upon failure, an errno code is returned.
597 *
598 * The number of bytes successfully read is returned through the length
599 * parameter by reference.  If both the return value and length parameter are
600 * returned as 0, then EOF was encountered immediately
601 *****************************************************************************/
[178]602uint32_t regfi_read(REGFI_RAW_FILE* file_cb, uint8_t* buf, uint32_t* length)
[101]603{
[168]604  uint32_t rsize = 0;
605  uint32_t rret = 0;
[101]606
607  do
608  {
[225]609    rret = file_cb->read(file_cb, 
610                         buf + rsize, 
611                         *length - rsize);
[101]612    if(rret > 0)
613      rsize += rret;
614  }while(*length - rsize > 0 
615         && (rret > 0 || (rret == -1 && (errno == EAGAIN || errno == EINTR))));
616 
617  *length = rsize;
618  if (rret == -1 && errno != EINTR && errno != EAGAIN)
619    return errno;
620
621  return 0;
622}
623
624
625/*****************************************************************************
626 *
627 *****************************************************************************/
[178]628bool regfi_parse_cell(REGFI_RAW_FILE* file_cb, uint32_t offset, uint8_t* hdr, 
629                      uint32_t hdr_len, uint32_t* cell_length, bool* unalloc)
[101]630{
[168]631  uint32_t length;
632  int32_t raw_length;
633  uint8_t tmp[4];
[101]634
[178]635  if(regfi_seek(file_cb, offset, SEEK_SET) == -1)
[101]636    return false;
637
638  length = 4;
[178]639  if((regfi_read(file_cb, tmp, &length) != 0) || length != 4)
[101]640    return false;
641  raw_length = IVALS(tmp, 0);
642
643  if(raw_length < 0)
644  {
645    (*cell_length) = raw_length*(-1);
646    (*unalloc) = false;
647  }
648  else
649  {
650    (*cell_length) = raw_length;
651    (*unalloc) = true;
652  }
653
[103]654  if(*cell_length - 4 < hdr_len)
655    return false;
656
657  if(hdr_len > 0)
658  {
659    length = hdr_len;
[178]660    if((regfi_read(file_cb, hdr, &length) != 0) || length != hdr_len)
[103]661      return false;
662  }
663
[101]664  return true;
665}
666
667
[157]668/******************************************************************************
[106]669 * Given an offset and an hbin, is the offset within that hbin?
670 * The offset is a virtual file offset.
[157]671 ******************************************************************************/
[168]672static bool regfi_offset_in_hbin(const REGFI_HBIN* hbin, uint32_t voffset)
[30]673{
[106]674  if(!hbin)
[31]675    return false;
[106]676
[145]677  if((voffset > hbin->first_hbin_off) 
678     && (voffset < (hbin->first_hbin_off + hbin->block_size)))
[31]679    return true;
[30]680               
[31]681  return false;
[30]682}
683
684
[106]685
[157]686/******************************************************************************
687 * Provide a physical offset and receive the correpsonding HBIN
[106]688 * block for it.  NULL if one doesn't exist.
[157]689 ******************************************************************************/
[168]690const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset)
[30]691{
[157]692  return (const REGFI_HBIN*)range_list_find_data(file->hbins, offset);
[30]693}
694
695
[157]696/******************************************************************************
697 * Calculate the largest possible cell size given a physical offset.
698 * Largest size is based on the HBIN the offset is currently a member of.
699 * Returns negative values on error.
700 * (Since cells can only be ~2^31 in size, this works out.)
701 ******************************************************************************/
[168]702int32_t regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset)
[157]703{
704  const REGFI_HBIN* hbin = regfi_lookup_hbin(file, offset);
705  if(hbin == NULL)
706    return -1;
[139]707
[157]708  return (hbin->block_size + hbin->file_off) - offset;
709}
710
711
[139]712/******************************************************************************
713 ******************************************************************************/
[168]714REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset, 
715                                         uint32_t num_keys, uint32_t max_size, 
[139]716                                         bool strict)
[127]717{
[135]718  REGFI_SUBKEY_LIST* ret_val;
[134]719
[139]720  ret_val = regfi_load_subkeylist_aux(file, offset, max_size, strict, 
721                                      REGFI_MAX_SUBKEY_DEPTH);
[143]722  if(ret_val == NULL)
723  {
[182]724    regfi_log_add(REGFI_LOG_WARN, "Failed to load subkey list at"
[143]725                      " offset 0x%.8X.", offset);
726    return NULL;
727  }
[139]728
729  if(num_keys != ret_val->num_keys)
730  {
731    /*  Not sure which should be authoritative, the number from the
732     *  NK record, or the number in the subkey list.  Just emit a warning for
733     *  now if they don't match.
734     */
[182]735    regfi_log_add(REGFI_LOG_WARN, "Number of subkeys listed in parent"
[139]736                      " (%d) did not match number found in subkey list/tree (%d)"
737                      " while parsing subkey list/tree at offset 0x%.8X.", 
738                      num_keys, ret_val->num_keys, offset);
739  }
740
741  return ret_val;
742}
743
744
745/******************************************************************************
746 ******************************************************************************/
[168]747REGFI_SUBKEY_LIST* regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset, 
748                                             uint32_t max_size, bool strict,
749                                             uint8_t depth_left)
[139]750{
751  REGFI_SUBKEY_LIST* ret_val;
752  REGFI_SUBKEY_LIST** sublists;
[168]753  uint32_t i, num_sublists, off;
754  int32_t sublist_maxsize;
[139]755
756  if(depth_left == 0)
757  {
[182]758    regfi_log_add(REGFI_LOG_WARN, "Maximum depth reached"
[139]759                      " while parsing subkey list/tree at offset 0x%.8X.", 
760                      offset);
[127]761    return NULL;
[139]762  }
[134]763
[139]764  ret_val = regfi_parse_subkeylist(file, offset, max_size, strict);
[134]765  if(ret_val == NULL)
766    return NULL;
[139]767
768  if(ret_val->recursive_type)
[127]769  {
[139]770    num_sublists = ret_val->num_children;
[150]771    sublists = (REGFI_SUBKEY_LIST**)malloc(num_sublists
[139]772                                           * sizeof(REGFI_SUBKEY_LIST*));
773    for(i=0; i < num_sublists; i++)
[127]774    {
[139]775      off = ret_val->elements[i].offset + REGFI_REGF_SIZE;
[157]776
777      sublist_maxsize = regfi_calc_maxsize(file, off);
778      if(sublist_maxsize < 0)
[139]779        sublists[i] = NULL;
780      else
[157]781        sublists[i] = regfi_load_subkeylist_aux(file, off, sublist_maxsize, 
782                                                strict, depth_left-1);
[127]783    }
[150]784    talloc_free(ret_val);
[134]785
[139]786    return regfi_merge_subkeylists(num_sublists, sublists, strict);
[127]787  }
[30]788
[127]789  return ret_val;
790}
791
792
[139]793/******************************************************************************
794 ******************************************************************************/
[168]795REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset, 
796                                          uint32_t max_size, bool strict)
[30]797{
[135]798  REGFI_SUBKEY_LIST* ret_val;
[168]799  uint32_t i, cell_length, length, elem_size, read_len;
800  uint8_t* elements = NULL;
801  uint8_t buf[REGFI_SUBKEY_LIST_MIN_LEN];
[104]802  bool unalloc;
[139]803  bool recursive_type;
[30]804
[186]805  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_subkeylist"))
[180]806     goto fail;
807
[178]808  if(!regfi_parse_cell(file->cb, offset, buf, REGFI_SUBKEY_LIST_MIN_LEN,
[104]809                       &cell_length, &unalloc))
[139]810  {
[182]811    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while "
[139]812                      "parsing subkey-list at offset 0x%.8X.", offset);
[180]813    goto fail_locked;
[139]814  }
[30]815
[116]816  if(cell_length > max_size)
817  {
[182]818    regfi_log_add(REGFI_LOG_WARN, "Cell size longer than max_size"
[139]819                      " while parsing subkey-list at offset 0x%.8X.", offset);
[116]820    if(strict)
[180]821      goto fail_locked;
[116]822    cell_length = max_size & 0xFFFFFFF8;
823  }
[30]824
[139]825  recursive_type = false;
[127]826  if(buf[0] == 'r' && buf[1] == 'i')
[104]827  {
[139]828    recursive_type = true;
[168]829    elem_size = sizeof(uint32_t);
[104]830  }
[139]831  else if(buf[0] == 'l' && buf[1] == 'i')
[203]832  {
[168]833    elem_size = sizeof(uint32_t);
[203]834  }
[134]835  else if((buf[0] == 'l') && (buf[1] == 'f' || buf[1] == 'h'))
[135]836    elem_size = sizeof(REGFI_SUBKEY_LIST_ELEM);
[134]837  else
838  {
[182]839    regfi_log_add(REGFI_LOG_ERROR, "Unknown magic number"
[139]840                      " (0x%.2X, 0x%.2X) encountered while parsing"
841                      " subkey-list at offset 0x%.8X.", buf[0], buf[1], offset);
[180]842    goto fail_locked;
[134]843  }
844
[150]845  ret_val = talloc(NULL, REGFI_SUBKEY_LIST);
[127]846  if(ret_val == NULL)
[180]847    goto fail_locked;
[127]848
849  ret_val->offset = offset;
850  ret_val->cell_size = cell_length;
[104]851  ret_val->magic[0] = buf[0];
852  ret_val->magic[1] = buf[1];
[139]853  ret_val->recursive_type = recursive_type;
854  ret_val->num_children = SVAL(buf, 0x2);
[101]855
[139]856  if(!recursive_type)
857    ret_val->num_keys = ret_val->num_children;
[101]858
[139]859  length = elem_size*ret_val->num_children;
[168]860  if(cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32_t) < length)
[134]861  {
[182]862    regfi_log_add(REGFI_LOG_WARN, "Number of elements too large for"
[139]863                      " cell while parsing subkey-list at offset 0x%.8X.", 
864                      offset);
865    if(strict)
[180]866      goto fail_locked;
[168]867    length = cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32_t);
[134]868  }
[30]869
[150]870  ret_val->elements = talloc_array(ret_val, REGFI_SUBKEY_LIST_ELEM, 
871                                   ret_val->num_children);
[127]872  if(ret_val->elements == NULL)
[180]873    goto fail_locked;
[30]874
[168]875  elements = (uint8_t*)malloc(length);
[139]876  if(elements == NULL)
[180]877    goto fail_locked;
[30]878
[150]879  read_len = length;
[178]880  if(regfi_read(file->cb, elements, &read_len) != 0 || read_len!=length)
[180]881    goto fail_locked;
[30]882
[186]883  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_subkeylist"))
[180]884     goto fail;
885
[168]886  if(elem_size == sizeof(uint32_t))
[104]887  {
[139]888    for (i=0; i < ret_val->num_children; i++)
[134]889    {
[139]890      ret_val->elements[i].offset = IVAL(elements, i*elem_size);
[134]891      ret_val->elements[i].hash = 0;
892    }
[104]893  }
[134]894  else
895  {
[139]896    for (i=0; i < ret_val->num_children; i++)
[134]897    {
[139]898      ret_val->elements[i].offset = IVAL(elements, i*elem_size);
899      ret_val->elements[i].hash = IVAL(elements, i*elem_size+4);
[134]900    }
901  }
[139]902  free(elements);
[30]903
[104]904  return ret_val;
[150]905
[180]906 fail_locked:
[186]907  regfi_unlock(file, &file->cb_lock, "regfi_parse_subkeylist");
[150]908 fail:
909  if(elements != NULL)
910    free(elements);
911  talloc_free(ret_val);
912  return NULL;
[30]913}
914
915
[139]916/*******************************************************************
917 *******************************************************************/
[168]918REGFI_SUBKEY_LIST* regfi_merge_subkeylists(uint16_t num_lists, 
[139]919                                           REGFI_SUBKEY_LIST** lists,
920                                           bool strict)
921{
[168]922  uint32_t i,j,k;
[139]923  REGFI_SUBKEY_LIST* ret_val;
[102]924
[139]925  if(lists == NULL)
926    return NULL;
[150]927  ret_val = talloc(NULL, REGFI_SUBKEY_LIST);
[139]928
929  if(ret_val == NULL)
930    return NULL;
931 
932  /* Obtain total number of elements */
933  ret_val->num_keys = 0;
934  for(i=0; i < num_lists; i++)
935  {
936    if(lists[i] != NULL)
937      ret_val->num_keys += lists[i]->num_children;
938  }
939  ret_val->num_children = ret_val->num_keys;
940
941  if(ret_val->num_keys > 0)
942  {
[150]943    ret_val->elements = talloc_array(ret_val, REGFI_SUBKEY_LIST_ELEM,
944                                     ret_val->num_keys);
[139]945    k=0;
946
947    if(ret_val->elements != NULL)
948    {
949      for(i=0; i < num_lists; i++)
950      {
951        if(lists[i] != NULL)
952        {
953          for(j=0; j < lists[i]->num_keys; j++)
954          {
[150]955            ret_val->elements[k].hash = lists[i]->elements[j].hash;
956            ret_val->elements[k++].offset = lists[i]->elements[j].offset;
[139]957          }
958        }
959      }
960    }
961  }
962 
963  for(i=0; i < num_lists; i++)
[184]964    talloc_free(lists[i]);
[139]965  free(lists);
966
967  return ret_val;
968}
969
970
[147]971/******************************************************************************
972 *
973 ******************************************************************************/
[203]974REGFI_SK* regfi_parse_sk(REGFI_FILE* file, uint32_t offset, uint32_t max_size, 
[147]975                             bool strict)
[30]976{
[203]977  REGFI_SK* ret_val = NULL;
[168]978  uint8_t* sec_desc_buf = NULL;
979  uint32_t cell_length, length;
980  uint8_t sk_header[REGFI_SK_MIN_LENGTH];
[102]981  bool unalloc = false;
[30]982
[186]983  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_sk"))
[180]984     goto fail;
985
[178]986  if(!regfi_parse_cell(file->cb, offset, sk_header, REGFI_SK_MIN_LENGTH,
[102]987                       &cell_length, &unalloc))
[137]988  {
[182]989    regfi_log_add(REGFI_LOG_WARN, "Could not parse SK record cell"
[137]990                      " at offset 0x%.8X.", offset);
[180]991    goto fail_locked;
[137]992  }
[102]993   
994  if(sk_header[0] != 's' || sk_header[1] != 'k')
[137]995  {
[182]996    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing"
[138]997                      " SK record at offset 0x%.8X.", offset);
[180]998    goto fail_locked;
[137]999  }
1000
[203]1001  ret_val = talloc(NULL, REGFI_SK);
[102]1002  if(ret_val == NULL)
[180]1003    goto fail_locked;
[30]1004
[102]1005  ret_val->offset = offset;
[116]1006  /* XXX: Is there a way to be more conservative (shorter) with
1007   *      cell length when cell is unallocated?
[111]1008   */
[102]1009  ret_val->cell_size = cell_length;
[30]1010
[102]1011  if(ret_val->cell_size > max_size)
1012    ret_val->cell_size = max_size & 0xFFFFFFF8;
1013  if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) 
[157]1014     || (strict && (ret_val->cell_size & 0x00000007) != 0))
[102]1015  {
[182]1016    regfi_log_add(REGFI_LOG_WARN, "Invalid cell size found while"
[138]1017                      " parsing SK record at offset 0x%.8X.", offset);
[180]1018    goto fail_locked;
[102]1019  }
[30]1020
[102]1021  ret_val->magic[0] = sk_header[0];
1022  ret_val->magic[1] = sk_header[1];
[30]1023
[102]1024  ret_val->unknown_tag = SVAL(sk_header, 0x2);
1025  ret_val->prev_sk_off = IVAL(sk_header, 0x4);
1026  ret_val->next_sk_off = IVAL(sk_header, 0x8);
1027  ret_val->ref_count = IVAL(sk_header, 0xC);
1028  ret_val->desc_size = IVAL(sk_header, 0x10);
[30]1029
[157]1030  if((ret_val->prev_sk_off & 0x00000007) != 0
1031     || (ret_val->next_sk_off & 0x00000007) != 0)
[140]1032  {
[182]1033    regfi_log_add(REGFI_LOG_WARN, "SK record's next/previous offsets"
[140]1034                      " are not a multiple of 8 while parsing SK record at"
1035                      " offset 0x%.8X.", offset);
[180]1036    goto fail_locked;
[140]1037  }
1038
[102]1039  if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size)
1040  {
[182]1041    regfi_log_add(REGFI_LOG_WARN, "Security descriptor too large for"
[138]1042                      " cell while parsing SK record at offset 0x%.8X.", 
1043                      offset);
[180]1044    goto fail_locked;
[102]1045  }
[30]1046
[168]1047  sec_desc_buf = (uint8_t*)malloc(ret_val->desc_size);
[147]1048  if(sec_desc_buf == NULL)
[180]1049    goto fail_locked;
[102]1050
[134]1051  length = ret_val->desc_size;
[178]1052  if(regfi_read(file->cb, sec_desc_buf, &length) != 0 
[134]1053     || length != ret_val->desc_size)
1054  {
[182]1055    regfi_log_add(REGFI_LOG_ERROR, "Failed to read security"
[138]1056                      " descriptor while parsing SK record at offset 0x%.8X.",
1057                      offset);
[180]1058    goto fail_locked;
[134]1059  }
[102]1060
[186]1061  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_sk"))
[180]1062     goto fail;
1063
[147]1064  if(!(ret_val->sec_desc = winsec_parse_desc(ret_val, sec_desc_buf, 
1065                                                   ret_val->desc_size)))
[134]1066  {
[182]1067    regfi_log_add(REGFI_LOG_ERROR, "Failed to parse security"
[138]1068                      " descriptor while parsing SK record at offset 0x%.8X.",
1069                      offset);
[147]1070    goto fail;
[134]1071  }
[147]1072
[134]1073  free(sec_desc_buf);
[147]1074  return ret_val;
[134]1075
[180]1076 fail_locked:
[186]1077  regfi_unlock(file, &file->cb_lock, "regfi_parse_sk");
[147]1078 fail:
1079  if(sec_desc_buf != NULL)
1080    free(sec_desc_buf);
1081  talloc_free(ret_val);
1082  return NULL;
[30]1083}
1084
1085
[168]1086REGFI_VALUE_LIST* regfi_parse_valuelist(REGFI_FILE* file, uint32_t offset, 
1087                                        uint32_t num_values, bool strict)
[111]1088{
[145]1089  REGFI_VALUE_LIST* ret_val;
[168]1090  uint32_t i, cell_length, length, read_len;
[111]1091  bool unalloc;
[30]1092
[186]1093  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_valuelist"))
[180]1094     goto fail;
1095
[178]1096  if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc))
[137]1097  {
[182]1098    regfi_log_add(REGFI_LOG_ERROR, "Failed to read cell header"
[137]1099                      " while parsing value list at offset 0x%.8X.", offset);
[180]1100    goto fail_locked;
[137]1101  }
[111]1102
[157]1103  if((cell_length & 0x00000007) != 0)
[111]1104  {
[182]1105    regfi_log_add(REGFI_LOG_WARN, "Cell length not a multiple of 8"
[145]1106                      " while parsing value list at offset 0x%.8X.", offset);
[111]1107    if(strict)
[180]1108      goto fail_locked;
[111]1109    cell_length = cell_length & 0xFFFFFFF8;
1110  }
[145]1111
[168]1112  if((num_values * sizeof(uint32_t)) > cell_length-sizeof(uint32_t))
[137]1113  {
[182]1114    regfi_log_add(REGFI_LOG_WARN, "Too many values found"
[137]1115                      " while parsing value list at offset 0x%.8X.", offset);
[145]1116    if(strict)
[180]1117      goto fail_locked;
[168]1118    num_values = cell_length/sizeof(uint32_t) - sizeof(uint32_t);
[137]1119  }
[111]1120
[168]1121  read_len = num_values*sizeof(uint32_t);
[150]1122  ret_val = talloc(NULL, REGFI_VALUE_LIST);
[111]1123  if(ret_val == NULL)
[180]1124    goto fail_locked;
[111]1125
[150]1126  ret_val->elements = (REGFI_VALUE_LIST_ELEM*)talloc_size(ret_val, read_len);
[145]1127  if(ret_val->elements == NULL)
[180]1128    goto fail_locked;
1129
[206]1130  ret_val->offset = offset;
1131  ret_val->cell_size = cell_length;
[145]1132  ret_val->num_values = num_values;
1133
[111]1134  length = read_len;
[178]1135  if((regfi_read(file->cb, (uint8_t*)ret_val->elements, &length) != 0) 
[145]1136     || length != read_len)
[111]1137  {
[182]1138    regfi_log_add(REGFI_LOG_ERROR, "Failed to read value pointers"
[137]1139                      " while parsing value list at offset 0x%.8X.", offset);
[180]1140    goto fail_locked;
[111]1141  }
1142 
[186]1143  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_valuelist"))
[180]1144     goto fail;
1145
[111]1146  for(i=0; i < num_values; i++)
1147  {
1148    /* Fix endianness */
[145]1149    ret_val->elements[i] = IVAL(&ret_val->elements[i], 0);
[111]1150
1151    /* Validate the first num_values values to ensure they make sense */
1152    if(strict)
1153    {
[145]1154      /* XXX: Need to revisit this file length check when we start dealing
1155       *      with partial files. */
1156      if((ret_val->elements[i] + REGFI_REGF_SIZE > file->file_length)
[157]1157         || ((ret_val->elements[i] & 0x00000007) != 0))
[111]1158      {
[182]1159        regfi_log_add(REGFI_LOG_WARN, "Invalid value pointer"
[138]1160                          " (0x%.8X) found while parsing value list at offset"
[145]1161                          " 0x%.8X.", ret_val->elements[i], offset);
[180]1162        goto fail;
[111]1163      }
1164    }
1165  }
1166
1167  return ret_val;
[180]1168
1169 fail_locked:
[186]1170  regfi_unlock(file, &file->cb_lock, "regfi_parse_valuelist");
[180]1171 fail:
1172  talloc_free(ret_val);
1173  return NULL;
[111]1174}
1175
[206]1176/* XXX: should give this boolean return type to indicate errors */
[203]1177void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, 
[162]1178                               REGFI_ENCODING output_encoding, bool strict)
[30]1179{
[165]1180  /* XXX: Registry value names are supposedly limited to 16383 characters
1181   *      according to:
1182   *      http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
1183   *      Might want to emit a warning if this is exceeded. 
1184   *      It is expected that "characters" could be variable width.
1185   *      Also, it may be useful to use this information to limit false positives
1186   *      when recovering deleted VK records.
1187   */
[172]1188  int32_t tmp_size;
1189  REGFI_ENCODING from_encoding = (vk->flags & REGFI_VK_FLAG_ASCIINAME)
[162]1190    ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE;
[151]1191
[256]1192  if(vk->name_length == 0)
1193    return;
1194
[162]1195  if(from_encoding == output_encoding)
1196  {
[206]1197    vk->name_raw[vk->name_length] = '\0';
1198    vk->name = (char*)vk->name_raw;
[162]1199  }
1200  else
1201  {
[206]1202    vk->name = talloc_array(vk, char, vk->name_length+1);
1203    if(vk->name == NULL)
[172]1204      return;
[162]1205
1206    tmp_size = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
1207                                  regfi_encoding_int2str(output_encoding),
[206]1208                                  vk->name_raw, vk->name,
[172]1209                                  vk->name_length, vk->name_length+1);
[162]1210    if(tmp_size < 0)
1211    {
[182]1212      regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
[206]1213                        " value name to encoding %s.  Error message: %s",
[162]1214                        regfi_encoding_int2str(output_encoding), 
1215                        strerror(-tmp_size));
[206]1216      talloc_free(vk->name);
1217      vk->name = NULL;
[162]1218    }
1219  }
[172]1220}
[162]1221
[172]1222
1223/******************************************************************************
1224 ******************************************************************************/
[203]1225REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
[206]1226                           REGFI_ENCODING output_encoding, bool strict)
[172]1227{
[203]1228  REGFI_VK* ret_val = NULL;
[172]1229  int32_t max_size;
1230
1231  max_size = regfi_calc_maxsize(file, offset);
1232  if(max_size < 0)
1233    return NULL;
1234 
1235  ret_val = regfi_parse_vk(file, offset, max_size, strict);
1236  if(ret_val == NULL)
1237    return NULL;
1238
1239  regfi_interpret_valuename(file, ret_val, output_encoding, strict);
1240
[103]1241  return ret_val;
[30]1242}
1243
1244
[145]1245/******************************************************************************
1246 * If !strict, the list may contain NULLs, VK records may point to NULL.
1247 ******************************************************************************/
[168]1248REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
1249                                       uint32_t num_values, uint32_t max_size,
[145]1250                                       bool strict)
1251{
[168]1252  uint32_t usable_num_values;
[30]1253
[168]1254  if((num_values+1) * sizeof(uint32_t) > max_size)
[145]1255  {
[182]1256    regfi_log_add(REGFI_LOG_WARN, "Number of values indicated by"
[145]1257                      " parent key (%d) would cause cell to straddle HBIN"
1258                      " boundary while loading value list at offset"
1259                      " 0x%.8X.", num_values, offset);
1260    if(strict)
1261      return NULL;
[168]1262    usable_num_values = max_size/sizeof(uint32_t) - sizeof(uint32_t);
[145]1263  }
1264  else
1265    usable_num_values = num_values;
1266
1267  return regfi_parse_valuelist(file, offset, usable_num_values, strict);
1268}
1269
1270
[206]1271/* XXX: should give this boolean return type to indicate errors */
[203]1272void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, 
[161]1273                             REGFI_ENCODING output_encoding, bool strict)
[30]1274{
[165]1275  /* XXX: Registry key names are supposedly limited to 255 characters according to:
1276   *      http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
1277   *      Might want to emit a warning if this is exceeded. 
1278   *      It is expected that "characters" could be variable width.
1279   *      Also, it may be useful to use this information to limit false positives
1280   *      when recovering deleted NK records.
1281   */
[172]1282  int32_t tmp_size;
1283  REGFI_ENCODING from_encoding = (nk->flags & REGFI_NK_FLAG_ASCIINAME) 
[161]1284    ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE;
[256]1285
1286  if(nk->name_length == 0)
1287    return; 
1288
[161]1289  if(from_encoding == output_encoding)
1290  {
[206]1291    nk->name_raw[nk->name_length] = '\0';
1292    nk->name = (char*)nk->name_raw;
[161]1293  }
1294  else
1295  {
[206]1296    nk->name = talloc_array(nk, char, nk->name_length+1);
1297    if(nk->name == NULL)
[172]1298      return;
[161]1299
[206]1300    memset(nk->name,0,nk->name_length+1);
1301
[161]1302    tmp_size = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
1303                                  regfi_encoding_int2str(output_encoding),
[206]1304                                  nk->name_raw, nk->name,
[161]1305                                  nk->name_length, nk->name_length+1);
1306    if(tmp_size < 0)
1307    {
[182]1308      regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
[206]1309                        " key name to encoding %s.  Error message: %s",
[161]1310                        regfi_encoding_int2str(output_encoding), 
1311                        strerror(-tmp_size));
[206]1312      talloc_free(nk->name);
1313      nk->name = NULL;
[161]1314    }
1315  }
[172]1316}
[161]1317
1318
[172]1319/******************************************************************************
1320 *
1321 ******************************************************************************/
[203]1322REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset,
[206]1323                         REGFI_ENCODING output_encoding, bool strict)
[172]1324{
[203]1325  REGFI_NK* nk;
[172]1326  uint32_t off;
1327  int32_t max_size;
1328
[250]1329  if(file->nk_cache != NULL)
1330  {
1331    /* First, check to see if we have this key in our cache */
1332    if(!regfi_lock(file, &file->mem_lock, "regfi_load_nk"))
1333      return NULL;
1334    regfi_lock(file, &file->nk_lock, "regfi_load_nk");
1335   
1336    nk = (REGFI_NK*)lru_cache_find(file->nk_cache, &offset, 4);
1337    if(nk != NULL)
1338      nk = talloc_reference(NULL, nk);
1339
1340    regfi_unlock(file, &file->nk_lock, "regfi_load_nk");
1341    regfi_unlock(file, &file->mem_lock, "regfi_load_nk");
1342    if(nk != NULL)
1343      return nk;
1344  }
1345
1346  /* Not cached currently, proceed with loading it */
[172]1347  max_size = regfi_calc_maxsize(file, offset);
1348  if (max_size < 0) 
1349    return NULL;
1350
1351  /* get the initial nk record */
1352  if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL)
1353  {
[182]1354    regfi_log_add(REGFI_LOG_ERROR, "Could not load NK record at"
1355                  " offset 0x%.8X.", offset);
[172]1356    return NULL;
1357  }
1358
1359  regfi_interpret_keyname(file, nk, output_encoding, strict);
1360
[146]1361  /* get value list */
[135]1362  if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) 
[32]1363  {
[157]1364    off = nk->values_off + REGFI_REGF_SIZE;
1365    max_size = regfi_calc_maxsize(file, off);
1366    if(max_size < 0)
[32]1367    {
[105]1368      if(strict)
[32]1369      {
[184]1370        talloc_free(nk);
[99]1371        return NULL;
[31]1372      }
[105]1373      else
1374        nk->values = NULL;
[31]1375    }
[105]1376    else
[103]1377    {
[157]1378      nk->values = regfi_load_valuelist(file, off, nk->num_values, 
1379                                        max_size, true);
[145]1380      if(nk->values == NULL)
[105]1381      {
[182]1382        regfi_log_add(REGFI_LOG_WARN, "Could not load value list"
1383                      " for NK record at offset 0x%.8X.", offset);
[145]1384        if(strict)
1385        {
[184]1386          talloc_free(nk);
[145]1387          return NULL;
1388        }
[105]1389      }
[223]1390      talloc_reparent(NULL, nk, nk->values);
[103]1391    }
[31]1392  }
[105]1393
[146]1394  /* now get subkey list */
[135]1395  if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) 
[32]1396  {
[157]1397    off = nk->subkeys_off + REGFI_REGF_SIZE;
1398    max_size = regfi_calc_maxsize(file, off);
1399    if(max_size < 0) 
[32]1400    {
[105]1401      if(strict)
[32]1402      {
[184]1403        talloc_free(nk);
[99]1404        return NULL;
[31]1405      }
[105]1406      else
1407        nk->subkeys = NULL;
[31]1408    }
[105]1409    else
[104]1410    {
[134]1411      nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys,
[157]1412                                          max_size, true);
[134]1413
[105]1414      if(nk->subkeys == NULL)
1415      {
[182]1416        regfi_log_add(REGFI_LOG_WARN, "Could not load subkey list"
1417                      " while parsing NK record at offset 0x%.8X.", offset);
[105]1418        nk->num_subkeys = 0;
1419      }
[223]1420      talloc_reparent(NULL, nk, nk->subkeys);
[104]1421    }
[31]1422  }
[30]1423
[250]1424  if(file->nk_cache != NULL)
1425  {
1426    /* All is well, so let us cache this key for later */
1427    if(!regfi_lock(file, &file->mem_lock, "regfi_load_nk"))
1428      return NULL;
1429    regfi_lock(file, &file->nk_lock, "regfi_load_nk");
1430   
1431    lru_cache_update(file->nk_cache, &offset, 4, nk);
1432   
1433    regfi_unlock(file, &file->nk_lock, "regfi_load_nk");
1434    regfi_unlock(file, &file->mem_lock, "regfi_load_nk");
1435  }
1436
[99]1437  return nk;
[30]1438}
1439
[32]1440
[102]1441/******************************************************************************
1442 ******************************************************************************/
[203]1443const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset, bool strict)
[146]1444{
[203]1445  REGFI_SK* ret_val = NULL;
[168]1446  int32_t max_size;
[147]1447  void* failure_ptr = NULL;
1448 
[184]1449  max_size = regfi_calc_maxsize(file, offset);
1450  if(max_size < 0)
1451    return NULL;
1452
1453  if(file->sk_cache == NULL)
1454    return regfi_parse_sk(file, offset, max_size, strict);
1455
[250]1456  if(!regfi_lock(file, &file->mem_lock, "regfi_load_sk"))
[180]1457    return NULL;
[250]1458  regfi_lock(file, &file->sk_lock, "regfi_load_sk");
[180]1459
[146]1460  /* First look if we have already parsed it */
[203]1461  ret_val = (REGFI_SK*)lru_cache_find(file->sk_cache, &offset, 4);
[146]1462
1463  /* Bail out if we have previously cached a parse failure at this offset. */
1464  if(ret_val == (void*)REGFI_OFFSET_NONE)
[250]1465  {
1466    ret_val = NULL;
1467    goto unlock;
1468  }
[146]1469
1470  if(ret_val == NULL)
1471  {
[157]1472    ret_val = regfi_parse_sk(file, offset, max_size, strict);
[146]1473    if(ret_val == NULL)
1474    { /* Cache the parse failure and bail out. */
[147]1475      failure_ptr = talloc(NULL, uint32_t);
1476      if(failure_ptr == NULL)
[250]1477        goto unlock;
1478
[147]1479      *(uint32_t*)failure_ptr = REGFI_OFFSET_NONE;
1480      lru_cache_update(file->sk_cache, &offset, 4, failure_ptr);
[184]1481
1482      /* Let the cache be the only owner of this */
1483      talloc_unlink(NULL, failure_ptr);
[146]1484    }
1485  }
[253]1486  else
1487    ret_val = talloc_reference(NULL, ret_val);
[146]1488
[250]1489 unlock:
1490  regfi_unlock(file, &file->sk_lock, "regfi_load_sk");
1491  regfi_unlock(file, &file->mem_lock, "regfi_load_sk");
[180]1492
[146]1493  return ret_val;
1494}
1495
1496
1497
1498/******************************************************************************
1499 ******************************************************************************/
[203]1500REGFI_NK* regfi_find_root_nk(REGFI_FILE* file, const REGFI_HBIN* hbin, 
[206]1501                             REGFI_ENCODING output_encoding)
[30]1502{
[203]1503  REGFI_NK* nk = NULL;
[168]1504  uint32_t cell_length;
1505  uint32_t cur_offset = hbin->file_off+REGFI_HBIN_HEADER_SIZE;
1506  uint32_t hbin_end = hbin->file_off+hbin->block_size;
[158]1507  bool unalloc;
[30]1508
[158]1509  while(cur_offset < hbin_end)
[32]1510  {
[180]1511
[186]1512    if(!regfi_lock(file, &file->cb_lock, "regfi_find_root_nk"))
[180]1513      return NULL;
1514
[178]1515    if(!regfi_parse_cell(file->cb, cur_offset, NULL, 0, &cell_length, &unalloc))
[158]1516    {
[182]1517      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell at offset"
1518                    " 0x%.8X while searching for root key.", cur_offset);
[255]1519      goto error_locked;
[158]1520    }
[180]1521
[186]1522    if(!regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk"))
[180]1523      return NULL;
1524
[158]1525    if(!unalloc)
[102]1526    {
[161]1527      nk = regfi_load_key(file, cur_offset, output_encoding, true);
[102]1528      if(nk != NULL)
1529      {
[161]1530        if(nk->flags & REGFI_NK_FLAG_ROOT)
[158]1531          return nk;
[102]1532      }
[31]1533    }
[30]1534
[158]1535    cur_offset += cell_length;
[31]1536  }
[32]1537
[158]1538  return NULL;
[255]1539
1540 error_locked:
1541  regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk");
1542  return NULL;
[30]1543}
1544
1545
[178]1546
[166]1547/******************************************************************************
1548 ******************************************************************************/
[206]1549REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding)
[30]1550{
[166]1551  REGFI_FILE* ret_val;
[178]1552  REGFI_RAW_FILE* file_cb = talloc(NULL, REGFI_RAW_FILE);
1553  if(file_cb == NULL) 
[31]1554    return NULL;
[166]1555
[178]1556  file_cb->state = (void*)talloc(file_cb, int);
1557  if(file_cb->state == NULL)
1558    goto fail;
1559  *(int*)file_cb->state = fd;
1560 
1561  file_cb->cur_off = 0;
1562  file_cb->size = 0;
1563  file_cb->read = &regfi_raw_read;
1564  file_cb->seek = &regfi_raw_seek;
1565 
[206]1566  ret_val = regfi_alloc_cb(file_cb, output_encoding);
[166]1567  if(ret_val == NULL)
[178]1568    goto fail;
[166]1569
[178]1570  /* In this case, we want file_cb to be freed when ret_val is */
[223]1571  talloc_reparent(NULL, ret_val, file_cb);
[166]1572  return ret_val;
[178]1573
1574 fail:
1575    talloc_free(file_cb);
1576    return NULL;
[166]1577}
1578
1579
[186]1580/******************************************************************************
1581 ******************************************************************************/
[223]1582static int regfi_free_cb(void* f)
[186]1583{
1584  REGFI_FILE* file = (REGFI_FILE*)f;
[178]1585
[186]1586  pthread_mutex_destroy(&file->cb_lock);
1587  pthread_rwlock_destroy(&file->hbins_lock);
1588  pthread_mutex_destroy(&file->sk_lock);
[250]1589  pthread_mutex_destroy(&file->nk_lock);
1590  pthread_mutex_destroy(&file->mem_lock);
[186]1591
1592  return 0;
1593}
1594
1595
1596/******************************************************************************
1597 ******************************************************************************/
[206]1598REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb, 
1599                           REGFI_ENCODING output_encoding)
[166]1600{
1601  REGFI_FILE* rb;
1602  REGFI_HBIN* hbin = NULL;
[178]1603  uint32_t hbin_off, cache_secret;
[226]1604  int64_t file_length;
[166]1605  bool rla;
1606
[178]1607  /* Determine file length.  Must be at least big enough for the header
1608   * and one hbin.
[137]1609   */
[226]1610  file_length = regfi_seek(file_cb, 0, SEEK_END);
[137]1611  if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC)
[182]1612  {
1613    regfi_log_add(REGFI_LOG_ERROR, "File length (%d) too short to contain a"
1614                  " header and at least one HBIN.", file_length);
[137]1615    return NULL;
[182]1616  }
[226]1617  regfi_seek(file_cb, 0, SEEK_SET);
[137]1618
[206]1619  if(output_encoding != REGFI_ENCODING_UTF8
1620     && output_encoding != REGFI_ENCODING_ASCII)
1621  { 
1622    regfi_log_add(REGFI_LOG_ERROR, "Invalid output_encoding supplied"
1623                  " in creation of regfi iterator.");
1624    return NULL;
1625  }
1626
[166]1627  /* Read file header */
[203]1628  if ((rb = regfi_parse_regf(file_cb, false)) == NULL)
[97]1629  {
[182]1630    regfi_log_add(REGFI_LOG_ERROR, "Failed to read REGF block.");
[31]1631    return NULL;
1632  }
[203]1633  rb->file_length = file_length;
[178]1634  rb->cb = file_cb;
[206]1635  rb->string_encoding = output_encoding;
[137]1636
[186]1637  if(pthread_mutex_init(&rb->cb_lock, NULL) != 0)
[182]1638  {
1639    regfi_log_add(REGFI_LOG_ERROR, "Failed to create cb_lock mutex.");
[180]1640    goto fail;
[182]1641  }
[180]1642
[186]1643  if(pthread_rwlock_init(&rb->hbins_lock, NULL) != 0)
[182]1644  {
1645    regfi_log_add(REGFI_LOG_ERROR, "Failed to create hbins_lock rwlock.");
[180]1646    goto fail;
[182]1647  }
[180]1648
[186]1649  if(pthread_mutex_init(&rb->sk_lock, NULL) != 0)
[182]1650  {
1651    regfi_log_add(REGFI_LOG_ERROR, "Failed to create sk_lock mutex.");
[180]1652    goto fail;
[182]1653  }
[180]1654
[250]1655  if(pthread_mutex_init(&rb->nk_lock, NULL) != 0)
1656  {
1657    regfi_log_add(REGFI_LOG_ERROR, "Failed to create nk_lock mutex.");
1658    goto fail;
1659  }
1660
[228]1661  if(pthread_mutex_init(&rb->mem_lock, NULL) != 0)
1662  {
1663    regfi_log_add(REGFI_LOG_ERROR, "Failed to create mem_lock mutex.");
1664    goto fail;
1665  }
1666
[99]1667  rb->hbins = range_list_new();
[110]1668  if(rb->hbins == NULL)
[182]1669  {
1670    regfi_log_add(REGFI_LOG_ERROR, "Failed to create HBIN range_list.");
[180]1671    goto fail;
[182]1672  }
[223]1673  talloc_reparent(NULL, rb, rb->hbins);
[150]1674
[106]1675  rla = true;
[135]1676  hbin_off = REGFI_REGF_SIZE;
[110]1677  hbin = regfi_parse_hbin(rb, hbin_off, true);
[106]1678  while(hbin && rla)
1679  {
[137]1680    rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin);
[148]1681    if(rla)
[223]1682      talloc_reparent(NULL, rb->hbins, hbin);
[180]1683
[106]1684    hbin_off = hbin->file_off + hbin->block_size;
[110]1685    hbin = regfi_parse_hbin(rb, hbin_off, true);
[106]1686  }
1687
[146]1688  /* This secret isn't very secret, but we don't need a good one.  This
1689   * secret is just designed to prevent someone from trying to blow our
1690   * caching and make things slow.
1691   */
1692  cache_secret = 0x15DEAD05^time(NULL)^(getpid()<<16);
1693
[250]1694  rb->sk_cache = NULL;
1695  if(REGFI_CACHE_SK_MAX > 0)
1696    rb->sk_cache = lru_cache_create_ctx(rb, REGFI_CACHE_SK_MAX, 
1697                                        cache_secret, true);
[146]1698
[250]1699  rb->nk_cache = NULL;
1700  if(REGFI_CACHE_NK_MAX > 0)
1701    rb->nk_cache = lru_cache_create_ctx(rb, REGFI_CACHE_NK_MAX, 
1702                                        cache_secret, true);
1703
[31]1704  /* success */
[186]1705  talloc_set_destructor(rb, regfi_free_cb);
[31]1706  return rb;
[180]1707
1708 fail:
[186]1709  pthread_mutex_destroy(&rb->cb_lock);
1710  pthread_rwlock_destroy(&rb->hbins_lock);
1711  pthread_mutex_destroy(&rb->sk_lock);
[250]1712  pthread_mutex_destroy(&rb->nk_lock);
[228]1713  pthread_mutex_destroy(&rb->mem_lock);
[180]1714
1715  range_list_free(rb->hbins);
1716  talloc_free(rb);
1717  return NULL;
[30]1718}
1719
1720
[148]1721/******************************************************************************
1722 ******************************************************************************/
[186]1723void regfi_free(REGFI_FILE* file)
[166]1724{
[186]1725  /* Callback handles cleanup side effects */
[150]1726  talloc_free(file);
[30]1727}
1728
1729
[80]1730/******************************************************************************
[158]1731 * First checks the offset given by the file header, then checks the
1732 * rest of the file if that fails.
[148]1733 ******************************************************************************/
[215]1734const REGFI_NK* regfi_get_rootkey(REGFI_FILE* file)
[30]1735{
[203]1736  REGFI_NK* nk = NULL;
[146]1737  REGFI_HBIN* hbin;
[168]1738  uint32_t root_offset, i, num_hbins;
[99]1739 
1740  if(!file)
[31]1741    return NULL;
[99]1742
[158]1743  root_offset = file->root_cell+REGFI_REGF_SIZE;
[206]1744  nk = regfi_load_key(file, root_offset, file->string_encoding, true);
[158]1745  if(nk != NULL)
1746  {
[161]1747    if(nk->flags & REGFI_NK_FLAG_ROOT)
[158]1748      return nk;
1749  }
1750
[182]1751  regfi_log_add(REGFI_LOG_WARN, "File header indicated root key at"
1752                " location 0x%.8X, but no root key found."
1753                " Searching rest of file...", root_offset);
[158]1754 
1755  /* If the file header gives bad info, scan through the file one HBIN
1756   * block at a time looking for an NK record with a root key type.
[146]1757   */
[180]1758 
[215]1759  if(!regfi_read_lock(file, &file->hbins_lock, "regfi_get_rootkey"))
[180]1760    return NULL;
1761
[107]1762  num_hbins = range_list_size(file->hbins);
[158]1763  for(i=0; i < num_hbins && nk == NULL; i++)
[99]1764  {
[135]1765    hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data;
[206]1766    nk = regfi_find_root_nk(file, hbin, file->string_encoding);
[31]1767  }
[30]1768
[215]1769  if(!regfi_rw_unlock(file, &file->hbins_lock, "regfi_get_rootkey"))
[180]1770    return NULL;
1771
[80]1772  return nk;
[30]1773}
1774
1775
[80]1776/******************************************************************************
1777 *****************************************************************************/
[228]1778void regfi_free_record(REGFI_FILE* file, const void* record)
[30]1779{
[228]1780  if(!regfi_lock(file, &file->mem_lock, "regfi_free_record"))
1781    return;
1782
[184]1783  talloc_unlink(NULL, (void*)record);
[228]1784
1785  regfi_unlock(file, &file->mem_lock, "regfi_free_record");
[150]1786}
[127]1787
[80]1788
[224]1789/******************************************************************************
1790 *****************************************************************************/
[252]1791const void* regfi_reference_record(REGFI_FILE* file, const void* record)
[224]1792{
[252]1793  const void* ret_val = NULL;
1794
[228]1795  if(!regfi_lock(file, &file->mem_lock, "regfi_reference_record"))
1796    return ret_val;
1797
[252]1798  ret_val = talloc_reference(NULL, record);
1799
[228]1800  regfi_unlock(file, &file->mem_lock, "regfi_reference_record");
1801  return ret_val;
[224]1802}
[80]1803
[207]1804
[80]1805/******************************************************************************
1806 *****************************************************************************/
[207]1807uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key)
1808{
1809  uint32_t num_in_list = 0;
[215]1810  if(key == NULL)
1811    return 0;
1812
[207]1813  if(key->subkeys != NULL)
1814    num_in_list = key->subkeys->num_keys;
1815
1816  if(num_in_list != key->num_subkeys)
1817  {
1818    regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d keys in its"
1819                  " subkey list but reports %d should be available.", 
1820                  key->offset, num_in_list, key->num_subkeys);
1821    return (num_in_list < key->num_subkeys)?num_in_list:key->num_subkeys;
1822  }
1823 
1824  return num_in_list;
1825}
1826
1827
1828/******************************************************************************
1829 *****************************************************************************/
1830uint32_t regfi_fetch_num_values(const REGFI_NK* key)
1831{
1832  uint32_t num_in_list = 0;
[215]1833  if(key == NULL)
1834    return 0;
1835
[207]1836  if(key->values != NULL)
1837    num_in_list = key->values->num_values;
1838
1839  if(num_in_list != key->num_values)
1840  {
1841    regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d values in"
1842                  " its value list but reports %d should be available.",
1843                  key->offset, num_in_list, key->num_values);
1844    return (num_in_list < key->num_values)?num_in_list:key->num_values;
1845  }
1846 
1847  return num_in_list;
1848}
1849
1850
1851/******************************************************************************
1852 *****************************************************************************/
[206]1853REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file)
[80]1854{
[203]1855  REGFI_NK* root;
[161]1856  REGFI_ITERATOR* ret_val;
1857
1858  ret_val = talloc(NULL, REGFI_ITERATOR);
[80]1859  if(ret_val == NULL)
1860    return NULL;
[249]1861 
1862  ret_val->cur = talloc(ret_val, REGFI_ITER_POSITION);
1863  if(ret_val->cur == NULL)
[80]1864  {
[150]1865    talloc_free(ret_val);
[80]1866    return NULL;
1867  }
1868
[135]1869  ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH);
[80]1870  if(ret_val->key_positions == NULL)
1871  {
[150]1872    talloc_free(ret_val);
[80]1873    return NULL;
1874  }
[223]1875  talloc_reparent(NULL, ret_val, ret_val->key_positions);
[80]1876
[249]1877  root = (REGFI_NK*)regfi_get_rootkey(file);
1878  if(root == NULL)
1879  {
1880    talloc_free(ret_val);
1881    return NULL;
1882  }
1883
1884  ret_val->cur->offset = root->offset;
1885  if(root->subkeys_off == REGFI_OFFSET_NONE)
1886    ret_val->cur->num_subkeys = 0;
1887  else
1888    ret_val->cur->num_subkeys = regfi_fetch_num_subkeys(root);
1889 
1890  if(root->values_off == REGFI_OFFSET_NONE)
1891    ret_val->cur->num_values = 0;
1892  else
1893    ret_val->cur->num_values = regfi_fetch_num_values(root);
1894
1895  ret_val->cur->cur_subkey = 0;
1896  ret_val->cur->cur_value = 0;
[159]1897  ret_val->f = file;
[249]1898
1899  regfi_free_record(ret_val->f, root);
[80]1900  return ret_val;
1901}
1902
1903
1904/******************************************************************************
1905 *****************************************************************************/
1906void regfi_iterator_free(REGFI_ITERATOR* i)
1907{
[228]1908  talloc_unlink(NULL, i);
[80]1909}
1910
1911
1912/******************************************************************************
1913 *****************************************************************************/
1914/* XXX: some way of indicating reason for failure should be added. */
1915bool regfi_iterator_down(REGFI_ITERATOR* i)
1916{
[203]1917  REGFI_NK* subkey;
[249]1918  REGFI_ITER_POSITION* pos = talloc(i, REGFI_ITER_POSITION);
[80]1919  if(pos == NULL)
1920    return false;
1921
[203]1922  subkey = (REGFI_NK*)regfi_iterator_cur_subkey(i);
[80]1923  if(subkey == NULL)
1924  {
[150]1925    talloc_free(pos);
[80]1926    return false;
1927  }
1928
[249]1929  if(!void_stack_push(i->key_positions, i->cur))
[80]1930  {
[150]1931    talloc_free(pos);
[249]1932    regfi_free_record(i->f, subkey);
[80]1933    return false;
[249]1934  }
[80]1935
[249]1936  pos->offset = subkey->offset;
1937  if(subkey->subkeys_off == REGFI_OFFSET_NONE)
1938    pos->num_subkeys = 0;
1939  else
1940    pos->num_subkeys = regfi_fetch_num_subkeys(subkey);
[80]1941
[249]1942  if(subkey->values_off == REGFI_OFFSET_NONE)
1943    pos->num_values = 0;
1944  else
1945    pos->num_values = regfi_fetch_num_values(subkey);
1946
1947  pos->cur_subkey = 0;
1948  pos->cur_value = 0;
1949  i->cur = pos;
1950
1951  regfi_free_record(i->f, subkey);
[80]1952  return true;
1953}
1954
1955
1956/******************************************************************************
1957 *****************************************************************************/
1958bool regfi_iterator_up(REGFI_ITERATOR* i)
1959{
1960  REGFI_ITER_POSITION* pos;
1961
1962  pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions);
1963  if(pos == NULL)
1964    return false;
1965
[228]1966  if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_iterator_up"))
1967    return false;
1968 
[249]1969  talloc_unlink(i, i->cur);
1970
[228]1971  regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_up");
1972
[249]1973  i->cur = pos;
[80]1974  return true;
1975}
1976
1977
1978/******************************************************************************
1979 *****************************************************************************/
1980bool regfi_iterator_to_root(REGFI_ITERATOR* i)
1981{
1982  while(regfi_iterator_up(i))
1983    continue;
1984
1985  return true;
1986}
1987
1988
1989/******************************************************************************
1990 *****************************************************************************/
[207]1991bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name)
[80]1992{
[249]1993  const REGFI_NK* cur_key;
[207]1994  uint32_t new_index;
[249]1995  bool ret_val = false;
[133]1996
[249]1997  cur_key = regfi_iterator_cur_key(i);
1998  if(cur_key == NULL)
[260]1999  {
[257]2000    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in find_subkey.");
[249]2001    return ret_val;
[260]2002  }
[249]2003
2004  if(regfi_find_subkey(i->f, cur_key, name, &new_index))
[80]2005  {
[249]2006    i->cur->cur_subkey = new_index;
2007    ret_val = true;
[80]2008  }
2009
[249]2010  regfi_free_record(i->f, cur_key);
2011  return ret_val;
[80]2012}
2013
2014
2015/******************************************************************************
2016 *****************************************************************************/
[252]2017bool regfi_iterator_descend(REGFI_ITERATOR* i, const char** path)
[80]2018{
[168]2019  uint32_t x;
[80]2020  if(path == NULL)
2021    return false;
2022
2023  for(x=0; 
2024      ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x])
2025       && regfi_iterator_down(i));
2026      x++)
2027  { continue; }
2028
2029  if(path[x] == NULL)
[215]2030  {
[80]2031    return true;
[215]2032  }
2033
[80]2034  /* XXX: is this the right number of times? */
2035  for(; x > 0; x--)
2036    regfi_iterator_up(i);
2037 
2038  return false;
2039}
2040
2041
2042/******************************************************************************
2043 *****************************************************************************/
[203]2044const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i)
[80]2045{
[228]2046  const REGFI_NK* ret_val = NULL;
2047
[249]2048  ret_val = regfi_load_key(i->f, i->cur->offset, i->f->string_encoding, true);
[228]2049  return ret_val;
[80]2050}
2051
2052
2053/******************************************************************************
2054 *****************************************************************************/
[206]2055const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key)
[109]2056{
[206]2057  if(key == NULL || key->sk_off == REGFI_OFFSET_NONE)
[109]2058    return NULL;
2059
[206]2060  return regfi_load_sk(file, key->sk_off + REGFI_REGF_SIZE, true);
[109]2061}
2062
2063
2064/******************************************************************************
2065 *****************************************************************************/
[253]2066const REGFI_SK* regfi_next_sk(REGFI_FILE* file, const REGFI_SK* sk)
2067{
2068  if(sk == NULL || sk->next_sk_off == REGFI_OFFSET_NONE)
2069    return NULL;
2070
2071  return regfi_load_sk(file, sk->next_sk_off + REGFI_REGF_SIZE, true);
2072}
2073
2074
2075/******************************************************************************
2076 *****************************************************************************/
2077const REGFI_SK* regfi_prev_sk(REGFI_FILE* file, const REGFI_SK* sk)
2078{
2079  if(sk == NULL || sk->prev_sk_off == REGFI_OFFSET_NONE)
2080    return NULL;
2081
2082  return regfi_load_sk(file, sk->prev_sk_off + REGFI_REGF_SIZE, true);
2083}
2084
2085
2086/******************************************************************************
2087 *****************************************************************************/
[199]2088bool regfi_iterator_first_subkey(REGFI_ITERATOR* i)
[80]2089{
[249]2090  i->cur->cur_subkey = 0;
2091  return (i->cur->cur_subkey < i->cur->num_subkeys);
[80]2092}
2093
2094
2095/******************************************************************************
2096 *****************************************************************************/
[203]2097const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i)
[80]2098{
[249]2099  const REGFI_NK* cur_key;
2100  const REGFI_NK* ret_val;
2101 
2102  cur_key = regfi_iterator_cur_key(i);
2103  if(cur_key == NULL)
[260]2104  {
[257]2105    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in cur_subkey.");
[249]2106    return NULL;
[260]2107  }
[249]2108
2109  ret_val = regfi_get_subkey(i->f, cur_key, i->cur->cur_subkey);
2110
2111  regfi_free_record(i->f, cur_key);
2112  return ret_val;
[30]2113}
[80]2114
2115
2116/******************************************************************************
2117 *****************************************************************************/
[199]2118bool regfi_iterator_next_subkey(REGFI_ITERATOR* i)
[80]2119{
[249]2120  i->cur->cur_subkey++;
2121  return (i->cur->cur_subkey < i->cur->num_subkeys);
[80]2122}
2123
2124
2125/******************************************************************************
2126 *****************************************************************************/
[207]2127bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name)
[80]2128{
[249]2129  const REGFI_NK* cur_key;
[207]2130  uint32_t new_index;
[249]2131  bool ret_val = false;
[80]2132
[249]2133  cur_key = regfi_iterator_cur_key(i);
2134  if(cur_key == NULL)
[260]2135  {
[257]2136    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in find_value.");
[249]2137    return ret_val;
[260]2138  }
[249]2139
2140  if(regfi_find_value(i->f, cur_key, name, &new_index))
[80]2141  {
[249]2142    i->cur->cur_value = new_index;
2143    ret_val = true;
[80]2144  }
2145
[249]2146  regfi_free_record(i->f, cur_key);
2147  return ret_val;
[80]2148}
2149
2150
2151/******************************************************************************
2152 *****************************************************************************/
[199]2153bool regfi_iterator_first_value(REGFI_ITERATOR* i)
[80]2154{
[249]2155  i->cur->cur_value = 0;
2156  return (i->cur->cur_value < i->cur->num_values);
[80]2157}
2158
2159
2160/******************************************************************************
2161 *****************************************************************************/
[203]2162const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i)
[80]2163{
[249]2164  const REGFI_NK* cur_key;
2165  const REGFI_VK* ret_val = NULL;
2166
2167  cur_key = regfi_iterator_cur_key(i);
2168  if(cur_key == NULL)
[260]2169  {
[257]2170    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in cur_value.");
[249]2171    return ret_val;
[260]2172  }
[249]2173
2174  ret_val = regfi_get_value(i->f, cur_key, i->cur->cur_value);
2175 
2176  regfi_free_record(i->f, cur_key);
2177  return ret_val;
[80]2178}
2179
2180
2181/******************************************************************************
2182 *****************************************************************************/
[199]2183bool regfi_iterator_next_value(REGFI_ITERATOR* i)
[80]2184{
[249]2185  i->cur->cur_value++;
2186  return (i->cur->cur_value < i->cur->num_values);
[80]2187}
[97]2188
2189
[249]2190
2191
[159]2192/******************************************************************************
2193 *****************************************************************************/
[252]2194const REGFI_NK** regfi_iterator_ancestry(REGFI_ITERATOR* i)
[249]2195{
2196  REGFI_NK** ret_val;
2197  void_stack_iterator* iter;
2198  const REGFI_ITER_POSITION* cur;
[250]2199  uint16_t k, num_keys;
[249]2200
[250]2201  num_keys = void_stack_size(i->key_positions)+1;
2202  ret_val = talloc_array(NULL, REGFI_NK*, num_keys+1);
[249]2203  if(ret_val == NULL)
2204    return NULL;
2205
2206  iter = void_stack_iterator_new(i->key_positions);
2207  if (iter == NULL)
2208  {
2209    talloc_free(ret_val);
2210    return NULL;
2211  }
[250]2212
2213  k=0;
2214  for(cur=void_stack_iterator_next(iter);
2215      cur != NULL; cur=void_stack_iterator_next(iter))
2216  { 
2217    ret_val[k++] = regfi_load_key(i->f, cur->offset, i->f->string_encoding, true); 
2218  }
2219  ret_val[k] = regfi_load_key(i->f, i->cur->offset, i->f->string_encoding, true);
2220  void_stack_iterator_free(iter);
2221
[252]2222  if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_iterator_ancestry"))
[249]2223  {
2224    talloc_free(ret_val);
2225    return NULL;
2226  }
2227
[250]2228  for(k=0; k<num_keys; k++)
[249]2229    talloc_reparent(NULL, ret_val, ret_val[k]);
2230
[252]2231  regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_ancestry");
[249]2232
[250]2233  ret_val[k] = NULL;
[249]2234  return (const REGFI_NK**)ret_val;
2235}
2236
2237
2238/******************************************************************************
2239 *****************************************************************************/
[206]2240const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file,
2241                                             const REGFI_NK* key)
[160]2242{
2243  REGFI_CLASSNAME* ret_val;
[168]2244  uint8_t* raw;
[160]2245  char* interpreted;
[168]2246  uint32_t offset;
2247  int32_t conv_size, max_size;
2248  uint16_t parse_length;
[160]2249
2250  if(key->classname_off == REGFI_OFFSET_NONE || key->classname_length == 0)
2251    return NULL;
2252
2253  offset = key->classname_off + REGFI_REGF_SIZE;
[206]2254  max_size = regfi_calc_maxsize(file, offset);
[160]2255  if(max_size <= 0)
2256    return NULL;
2257
2258  parse_length = key->classname_length;
[206]2259  raw = regfi_parse_classname(file, offset, &parse_length, max_size, true);
[160]2260 
2261  if(raw == NULL)
2262  {
[182]2263    regfi_log_add(REGFI_LOG_WARN, "Could not parse class"
2264                  " name at offset 0x%.8X for key record at offset 0x%.8X.",
2265                  offset, key->offset);
[160]2266    return NULL;
2267  }
2268
2269  ret_val = talloc(NULL, REGFI_CLASSNAME);
2270  if(ret_val == NULL)
2271    return NULL;
2272
[206]2273  ret_val->offset = offset;
[160]2274  ret_val->raw = raw;
2275  ret_val->size = parse_length;
[223]2276  talloc_reparent(NULL, ret_val, raw);
[160]2277
2278  interpreted = talloc_array(NULL, char, parse_length);
2279
[161]2280  conv_size = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
[206]2281                                 regfi_encoding_int2str(file->string_encoding),
[160]2282                                 raw, interpreted,
2283                                 parse_length, parse_length);
2284  if(conv_size < 0)
2285  {
[182]2286    regfi_log_add(REGFI_LOG_WARN, "Error occurred while"
2287                  " converting classname to charset %s.  Error message: %s",
[206]2288                  file->string_encoding, strerror(-conv_size));
[160]2289    talloc_free(interpreted);
2290    ret_val->interpreted = NULL;
2291  }
2292  else
2293  {
[223]2294    /* XXX: check for NULL return here? */
[160]2295    interpreted = talloc_realloc(NULL, interpreted, char, conv_size);
2296    ret_val->interpreted = interpreted;
[223]2297    talloc_reparent(NULL, ret_val, interpreted);
[160]2298  }
2299
2300  return ret_val;
2301}
2302
2303
2304/******************************************************************************
2305 *****************************************************************************/
[206]2306const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file, 
2307                                   const REGFI_VK* value)
[159]2308{
2309  REGFI_DATA* ret_val = NULL;
2310  REGFI_BUFFER raw_data;
2311
2312  if(value->data_size != 0)
2313  {
[206]2314    raw_data = regfi_load_data(file, value->data_off, value->data_size,
[209]2315                               value->data_in_offset, true);
[159]2316    if(raw_data.buf == NULL)
2317    {
[182]2318      regfi_log_add(REGFI_LOG_WARN, "Could not parse data record"
2319                    " while parsing VK record at offset 0x%.8X.",
2320                    value->offset);
[159]2321    }
2322    else
2323    {
2324      ret_val = regfi_buffer_to_data(raw_data);
2325
2326      if(ret_val == NULL)
2327      {
[182]2328        regfi_log_add(REGFI_LOG_WARN, "Error occurred in converting"
2329                      " data buffer to data structure while interpreting "
2330                      "data for VK record at offset 0x%.8X.",
2331                      value->offset);
[159]2332        talloc_free(raw_data.buf);
2333        return NULL;
2334      }
2335
[206]2336      if(!regfi_interpret_data(file, file->string_encoding, 
2337                               value->type, ret_val))
[159]2338      {
[182]2339        regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
2340                      " interpreting data for VK record at offset 0x%.8X.",
2341                      value->offset);
[159]2342      }
2343    }
2344  }
2345 
2346  return ret_val;
2347}
2348
2349
[207]2350
[159]2351/******************************************************************************
2352 *****************************************************************************/
[207]2353bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key, 
2354                       const char* name, uint32_t* index)
2355{
2356  const REGFI_NK* cur;
2357  uint32_t i;
2358  uint32_t num_subkeys = regfi_fetch_num_subkeys(key);
2359  bool found = false;
2360
[256]2361  /* XXX: should we allow "(default)" subkey names?
2362   *      Do realistically they exist?
[207]2363   */
2364  if(name == NULL)
2365    return false;
2366
2367  for(i=0; (i < num_subkeys) && (found == false); i++)
2368  {
2369    cur = regfi_get_subkey(file, key, i);
2370    if(cur == NULL)
2371      return false;
2372
[256]2373    /* A NULL name signifies the "(default)" value for a key */
2374    if(cur->name != NULL && (strcasecmp(cur->name, name) == 0))
[207]2375    {
2376      found = true;
2377      *index = i;
2378    }
2379
[228]2380    regfi_free_record(file, cur);
[207]2381  }
2382
2383  return found;
2384}
2385
2386
2387
2388/******************************************************************************
2389 *****************************************************************************/
2390bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key, 
2391                      const char* name, uint32_t* index)
2392{
2393  const REGFI_VK* cur;
2394  uint32_t i;
2395  uint32_t num_values = regfi_fetch_num_values(key);
2396  bool found = false;
2397
2398  for(i=0; (i < num_values) && (found == false); i++)
2399  {
2400    cur = regfi_get_value(file, key, i);
2401    if(cur == NULL)
2402      return false;
2403
[256]2404    /* A NULL name signifies the "(default)" value for a key */
2405    if(((name == NULL) && (cur->name == NULL))
2406       || ((name != NULL) && (cur->name != NULL) 
2407           && (strcasecmp(cur->name, name) == 0)))
[207]2408    {
2409      found = true;
2410      *index = i;
2411    }
2412
[228]2413    regfi_free_record(file, cur);
[207]2414  }
2415
2416  return found;
2417}
2418
2419
2420
2421/******************************************************************************
2422 *****************************************************************************/
2423const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key, 
2424                                 uint32_t index)
2425{
2426  if(index < regfi_fetch_num_subkeys(key))
2427  {
2428    return regfi_load_key(file, 
2429                          key->subkeys->elements[index].offset+REGFI_REGF_SIZE,
2430                          file->string_encoding, true);
2431  }
2432
2433  return NULL;
2434}
2435
2436
2437/******************************************************************************
2438 *****************************************************************************/
2439const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, 
2440                                uint32_t index)
2441{
2442  if(index < regfi_fetch_num_values(key))
2443  {
2444    return regfi_load_value(file, 
2445                            key->values->elements[index]+REGFI_REGF_SIZE,
2446                            file->string_encoding, true);
2447  }
2448
2449  return NULL; 
2450}
2451
2452
[215]2453
[207]2454/******************************************************************************
2455 *****************************************************************************/
[215]2456const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key)
2457{
2458  if(key != NULL && key->parent_off != REGFI_OFFSET_NONE)
2459    return regfi_load_key(file, 
2460                          key->parent_off+REGFI_REGF_SIZE,
2461                          file->string_encoding, true);
[228]2462
[215]2463  return NULL;
2464}
2465
2466
2467
2468/******************************************************************************
2469 *****************************************************************************/
[159]2470REGFI_DATA* regfi_buffer_to_data(REGFI_BUFFER raw_data)
2471{
2472  REGFI_DATA* ret_val;
2473
2474  if(raw_data.buf == NULL)
2475    return NULL;
2476
2477  ret_val = talloc(NULL, REGFI_DATA);
2478  if(ret_val == NULL)
2479    return NULL;
2480 
[223]2481  talloc_reparent(NULL, ret_val, raw_data.buf);
[159]2482  ret_val->raw = raw_data.buf;
2483  ret_val->size = raw_data.len;
2484  ret_val->interpreted_size = 0;
2485  ret_val->interpreted.qword = 0;
2486
2487  return ret_val;
2488}
2489
2490
2491/******************************************************************************
2492 *****************************************************************************/
[161]2493bool regfi_interpret_data(REGFI_FILE* file, REGFI_ENCODING string_encoding,
[168]2494                          uint32_t type, REGFI_DATA* data)
[159]2495{
[168]2496  uint8_t** tmp_array;
2497  uint8_t* tmp_str;
2498  int32_t tmp_size;
2499  uint32_t i, j, array_size;
[159]2500
2501  if(data == NULL)
2502    return false;
2503
2504  switch (type)
2505  {
2506  case REG_SZ:
2507  case REG_EXPAND_SZ:
2508  /* REG_LINK is a symbolic link, stored as a unicode string. */
2509  case REG_LINK:
[168]2510    tmp_str = talloc_array(NULL, uint8_t, data->size);
[159]2511    if(tmp_str == NULL)
2512    {
2513      data->interpreted.string = NULL;
2514      data->interpreted_size = 0;
2515      return false;
2516    }
2517     
[161]2518    tmp_size = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
2519                                  regfi_encoding_int2str(string_encoding),
[159]2520                                  data->raw, (char*)tmp_str, 
2521                                  data->size, data->size);
2522    if(tmp_size < 0)
2523    {
[182]2524      regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
[193]2525                    " converting data of type %d to %d.  Error message: %s",
[182]2526                    type, string_encoding, strerror(-tmp_size));
[159]2527      talloc_free(tmp_str);
2528      data->interpreted.string = NULL;
2529      data->interpreted_size = 0;
2530      return false;
2531    }
2532
[168]2533    tmp_str = talloc_realloc(NULL, tmp_str, uint8_t, tmp_size);
[257]2534    if(tmp_str == NULL)
2535      return false;
[159]2536    data->interpreted.string = tmp_str;
2537    data->interpreted_size = tmp_size;
[223]2538    talloc_reparent(NULL, data, tmp_str);
[159]2539    break;
2540
2541  case REG_DWORD:
2542    if(data->size < 4)
2543    {
2544      data->interpreted.dword = 0;
2545      data->interpreted_size = 0;
2546      return false;
2547    }
2548    data->interpreted.dword = IVAL(data->raw, 0);
2549    data->interpreted_size = 4;
2550    break;
2551
2552  case REG_DWORD_BE:
2553    if(data->size < 4)
2554    {
2555      data->interpreted.dword_be = 0;
2556      data->interpreted_size = 0;
2557      return false;
2558    }
2559    data->interpreted.dword_be = RIVAL(data->raw, 0);
2560    data->interpreted_size = 4;
2561    break;
2562
2563  case REG_QWORD:
2564    if(data->size < 8)
2565    {
2566      data->interpreted.qword = 0;
2567      data->interpreted_size = 0;
2568      return false;
2569    }
2570    data->interpreted.qword = 
[168]2571      (uint64_t)IVAL(data->raw, 0) + (((uint64_t)IVAL(data->raw, 4))<<32);
[159]2572    data->interpreted_size = 8;
2573    break;
2574   
2575  case REG_MULTI_SZ:
[168]2576    tmp_str = talloc_array(NULL, uint8_t, data->size);
[159]2577    if(tmp_str == NULL)
2578    {
2579      data->interpreted.multiple_string = NULL;
2580      data->interpreted_size = 0;
2581      return false;
2582    }
2583
2584    /* Attempt to convert entire string from UTF-16LE to output encoding,
2585     * then parse and quote fields individually.
2586     */
[161]2587    tmp_size = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
2588                                  regfi_encoding_int2str(string_encoding),
[159]2589                                  data->raw, (char*)tmp_str,
2590                                  data->size, data->size);
2591    if(tmp_size < 0)
2592    {
[182]2593      regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
2594                    " converting data of type %d to %s.  Error message: %s",
2595                    type, string_encoding, strerror(-tmp_size));
[159]2596      talloc_free(tmp_str);
2597      data->interpreted.multiple_string = NULL;
2598      data->interpreted_size = 0;
2599      return false;
2600    }
2601
2602    array_size = tmp_size+1;
[168]2603    tmp_array = talloc_array(NULL, uint8_t*, array_size);
[159]2604    if(tmp_array == NULL)
2605    {
2606      talloc_free(tmp_str);
2607      data->interpreted.string = NULL;
2608      data->interpreted_size = 0;
2609      return false;
2610    }
2611   
2612    tmp_array[0] = tmp_str;
2613    for(i=0,j=1; i < tmp_size && j < array_size-1; i++)
2614    {
[209]2615      if(tmp_str[i] == '\0' && (i+1 < tmp_size) && tmp_str[i+1] != '\0')
[159]2616        tmp_array[j++] = tmp_str+i+1;
2617    }
2618    tmp_array[j] = NULL;
[168]2619    tmp_array = talloc_realloc(NULL, tmp_array, uint8_t*, j+1);
[159]2620    data->interpreted.multiple_string = tmp_array;
2621    /* XXX: how meaningful is this?  should we store number of strings instead? */
2622    data->interpreted_size = tmp_size;
[223]2623    talloc_reparent(NULL, tmp_array, tmp_str);
2624    talloc_reparent(NULL, data, tmp_array);
[159]2625    break;
2626
2627  /* XXX: Dont know how to interpret these yet, just treat as binary */
2628  case REG_NONE:
2629    data->interpreted.none = data->raw;
2630    data->interpreted_size = data->size;
2631    break;
2632
2633  case REG_RESOURCE_LIST:
2634    data->interpreted.resource_list = data->raw;
2635    data->interpreted_size = data->size;
2636    break;
2637
2638  case REG_FULL_RESOURCE_DESCRIPTOR:
2639    data->interpreted.full_resource_descriptor = data->raw;
2640    data->interpreted_size = data->size;
2641    break;
2642
2643  case REG_RESOURCE_REQUIREMENTS_LIST:
2644    data->interpreted.resource_requirements_list = data->raw;
2645    data->interpreted_size = data->size;
2646    break;
2647
2648  case REG_BINARY:
2649    data->interpreted.binary = data->raw;
2650    data->interpreted_size = data->size;
2651    break;
2652
2653  default:
2654    data->interpreted.qword = 0;
2655    data->interpreted_size = 0;
2656    return false;
2657  }
2658
2659  data->type = type;
2660  return true;
2661}
2662
2663
[166]2664/******************************************************************************
[159]2665 * Convert from UTF-16LE to specified character set.
2666 * On error, returns a negative errno code.
[166]2667 *****************************************************************************/
[168]2668int32_t regfi_conv_charset(const char* input_charset, const char* output_charset,
[206]2669                           uint8_t* input, char* output, 
2670                           uint32_t input_len, uint32_t output_max)
[159]2671{
2672  iconv_t conv_desc;
2673  char* inbuf = (char*)input;
2674  char* outbuf = output;
2675  size_t in_len = (size_t)input_len;
2676  size_t out_len = (size_t)(output_max-1);
2677  int ret;
2678
[161]2679  /* XXX: Consider creating a couple of conversion descriptors earlier,
2680   *      storing them on an iterator so they don't have to be recreated
2681   *      each time.
2682   */
2683
[159]2684  /* Set up conversion descriptor. */
[161]2685  conv_desc = iconv_open(output_charset, input_charset);
[159]2686
2687  ret = iconv(conv_desc, &inbuf, &in_len, &outbuf, &out_len);
2688  if(ret == -1)
2689  {
2690    iconv_close(conv_desc);
2691    return -errno;
2692  }
2693  *outbuf = '\0';
2694
2695  iconv_close(conv_desc); 
2696  return output_max-out_len-1;
2697}
2698
2699
2700
2701/*******************************************************************
[97]2702 * Computes the checksum of the registry file header.
[159]2703 * buffer must be at least the size of a regf header (4096 bytes).
[97]2704 *******************************************************************/
[168]2705static uint32_t regfi_compute_header_checksum(uint8_t* buffer)
[97]2706{
[168]2707  uint32_t checksum, x;
[97]2708  int i;
2709
2710  /* XOR of all bytes 0x0000 - 0x01FB */
2711
2712  checksum = x = 0;
2713 
2714  for ( i=0; i<0x01FB; i+=4 ) {
2715    x = IVAL(buffer, i );
2716    checksum ^= x;
2717  }
2718 
2719  return checksum;
2720}
2721
2722
2723/*******************************************************************
2724 *******************************************************************/
[178]2725REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict)
[97]2726{
[168]2727  uint8_t file_header[REGFI_REGF_SIZE];
2728  uint32_t length;
[135]2729  REGFI_FILE* ret_val;
[97]2730
[150]2731  ret_val = talloc(NULL, REGFI_FILE);
[97]2732  if(ret_val == NULL)
2733    return NULL;
2734
[150]2735  ret_val->sk_cache = NULL;
2736  ret_val->hbins = NULL;
[178]2737
[135]2738  length = REGFI_REGF_SIZE;
[178]2739  if((regfi_read(file_cb, file_header, &length)) != 0 
2740     || length != REGFI_REGF_SIZE)
[182]2741  {
2742    regfi_log_add(REGFI_LOG_WARN, "Read failed while parsing REGF structure.");
[150]2743    goto fail;
[182]2744  }
2745
[97]2746  ret_val->checksum = IVAL(file_header, 0x1FC);
2747  ret_val->computed_checksum = regfi_compute_header_checksum(file_header);
2748  if (strict && (ret_val->checksum != ret_val->computed_checksum))
[182]2749  {
2750    regfi_log_add(REGFI_LOG_WARN, "Stored header checksum (%.8X) did not equal"
2751                  " computed checksum (%.8X).",
2752                  ret_val->checksum, ret_val->computed_checksum);
2753    if(strict)
2754      goto fail;
2755  }
[97]2756
[135]2757  memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE);
[150]2758  if(memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0)
[97]2759  {
[182]2760    regfi_log_add(REGFI_LOG_ERROR, "Magic number mismatch "
2761                  "(%.2X %.2X %.2X %.2X) while parsing hive header",
2762                  ret_val->magic[0], ret_val->magic[1], 
2763                  ret_val->magic[2], ret_val->magic[3]);
2764    goto fail;
[97]2765  }
[178]2766
[151]2767  ret_val->sequence1 = IVAL(file_header, 0x4);
2768  ret_val->sequence2 = IVAL(file_header, 0x8);
[251]2769  ret_val->mtime = ((uint64_t)IVAL(file_header, 0x10)) << 32;
2770  ret_val->mtime |= IVAL(file_header, 0xC);
[151]2771  ret_val->major_version = IVAL(file_header, 0x14);
2772  ret_val->minor_version = IVAL(file_header, 0x18);
2773  ret_val->type = IVAL(file_header, 0x1C);
2774  ret_val->format = IVAL(file_header, 0x20);
2775  ret_val->root_cell = IVAL(file_header, 0x24);
[97]2776  ret_val->last_block = IVAL(file_header, 0x28);
[151]2777  ret_val->cluster = IVAL(file_header, 0x2C);
[97]2778
[151]2779  memcpy(ret_val->file_name, file_header+0x30,  REGFI_REGF_NAME_SIZE);
2780
2781  ret_val->rm_id = winsec_parse_uuid(ret_val, file_header+0x70, 16);
[257]2782  if(ret_val->rm_id == NULL)
2783    regfi_log_add(REGFI_LOG_WARN, "Hive header's rm_id failed to parse.");
2784
[151]2785  ret_val->log_id = winsec_parse_uuid(ret_val, file_header+0x80, 16);
[257]2786  if(ret_val->log_id == NULL)
2787    regfi_log_add(REGFI_LOG_WARN, "Hive header's log_id failed to parse.");
2788
[151]2789  ret_val->flags = IVAL(file_header, 0x90);
[257]2790
[151]2791  ret_val->tm_id = winsec_parse_uuid(ret_val, file_header+0x94, 16);
[257]2792  if(ret_val->tm_id == NULL)
2793    regfi_log_add(REGFI_LOG_WARN, "Hive header's tm_id failed to parse.");
2794
[151]2795  ret_val->guid_signature = IVAL(file_header, 0xa4);
2796
2797  memcpy(ret_val->reserved1, file_header+0xa8, REGFI_REGF_RESERVED1_SIZE);
2798  memcpy(ret_val->reserved2, file_header+0x200, REGFI_REGF_RESERVED2_SIZE);
2799
2800  ret_val->thaw_tm_id = winsec_parse_uuid(ret_val, file_header+0xFC8, 16);
2801  ret_val->thaw_rm_id = winsec_parse_uuid(ret_val, file_header+0xFD8, 16);
2802  ret_val->thaw_log_id = winsec_parse_uuid(ret_val, file_header+0xFE8, 16);
[152]2803  ret_val->boot_type = IVAL(file_header, 0xFF8);
2804  ret_val->boot_recover = IVAL(file_header, 0xFFC);
[151]2805
[97]2806  return ret_val;
[150]2807
2808 fail:
2809  talloc_free(ret_val);
2810  return NULL;
[97]2811}
2812
2813
2814
[148]2815/******************************************************************************
[97]2816 * Given real file offset, read and parse the hbin at that location
[110]2817 * along with it's associated cells.
[148]2818 ******************************************************************************/
[168]2819REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, bool strict)
[97]2820{
[181]2821  REGFI_HBIN* hbin = NULL;
[168]2822  uint8_t hbin_header[REGFI_HBIN_HEADER_SIZE];
2823  uint32_t length;
[99]2824 
2825  if(offset >= file->file_length)
[180]2826    goto fail;
2827 
[186]2828  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_hbin"))
[180]2829    goto fail;
[97]2830
[178]2831  if(regfi_seek(file->cb, offset, SEEK_SET) == -1)
[137]2832  {
[182]2833    regfi_log_add(REGFI_LOG_ERROR, "Seek failed"
2834                  " while parsing hbin at offset 0x%.8X.", offset);
[180]2835    goto fail_locked;
[137]2836  }
[97]2837
[135]2838  length = REGFI_HBIN_HEADER_SIZE;
[178]2839  if((regfi_read(file->cb, hbin_header, &length) != 0) 
[135]2840     || length != REGFI_HBIN_HEADER_SIZE)
[182]2841  {
2842    regfi_log_add(REGFI_LOG_ERROR, "Read failed"
2843                  " while parsing hbin at offset 0x%.8X.", offset);
[180]2844    goto fail_locked;
[182]2845  }
[97]2846
[186]2847  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_hbin"))
[180]2848    goto fail;
[97]2849
[148]2850  hbin = talloc(NULL, REGFI_HBIN);
2851  if(hbin == NULL)
[180]2852    goto fail;
[99]2853  hbin->file_off = offset;
2854
[97]2855  memcpy(hbin->magic, hbin_header, 4);
2856  if(strict && (memcmp(hbin->magic, "hbin", 4) != 0))
[99]2857  {
[182]2858    /* This always seems to happen at the end of a file, so we make it an INFO
2859     * message, rather than something more serious.
2860     */
2861    regfi_log_add(REGFI_LOG_INFO, "Magic number mismatch "
2862                  "(%.2X %.2X %.2X %.2X) while parsing hbin at offset"
2863                  " 0x%.8X.", hbin->magic[0], hbin->magic[1], 
2864                  hbin->magic[2], hbin->magic[3], offset);
[180]2865    goto fail;
[99]2866  }
[97]2867
2868  hbin->first_hbin_off = IVAL(hbin_header, 0x4);
2869  hbin->block_size = IVAL(hbin_header, 0x8);
[182]2870  /* this should be the same thing as hbin->block_size, but just in case */
[97]2871  hbin->next_block = IVAL(hbin_header, 0x1C);
2872
2873
2874  /* Ensure the block size is a multiple of 0x1000 and doesn't run off
2875   * the end of the file.
2876   */
[116]2877  /* XXX: This may need to be relaxed for dealing with
2878   *      partial or corrupt files.
2879   */
[97]2880  if((offset + hbin->block_size > file->file_length)
2881     || (hbin->block_size & 0xFFFFF000) != hbin->block_size)
[99]2882  {
[182]2883    regfi_log_add(REGFI_LOG_ERROR, "The hbin offset is not aligned"
2884                  " or runs off the end of the file"
2885                  " while parsing hbin at offset 0x%.8X.", offset);
[180]2886    goto fail;
[99]2887  }
[97]2888
2889  return hbin;
[180]2890
2891 fail_locked:
[186]2892  regfi_unlock(file, &file->cb_lock, "regfi_parse_hbin");
[180]2893 fail:
2894  talloc_free(hbin);
2895  return NULL;
[97]2896}
2897
2898
[126]2899/*******************************************************************
2900 *******************************************************************/
[203]2901REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset, 
2902                         uint32_t max_size, bool strict)
[99]2903{
[168]2904  uint8_t nk_header[REGFI_NK_MIN_LENGTH];
[203]2905  REGFI_NK* ret_val;
[168]2906  uint32_t length,cell_length;
[101]2907  bool unalloc = false;
[99]2908
[203]2909  ret_val = talloc(NULL, REGFI_NK);
[180]2910  if(ret_val == NULL)
2911  {
[182]2912    regfi_log_add(REGFI_LOG_ERROR, "Failed to allocate memory while"
2913                  " parsing NK record at offset 0x%.8X.", offset);
[180]2914    goto fail;
2915  }
2916
[186]2917  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_nk"))
[180]2918    goto fail;
2919
[178]2920  if(!regfi_parse_cell(file->cb, offset, nk_header, REGFI_NK_MIN_LENGTH,
[101]2921                       &cell_length, &unalloc))
[137]2922  {
[182]2923    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
2924                  " while parsing NK record at offset 0x%.8X.", offset);
[180]2925    goto fail_locked;
[137]2926  }
2927
[101]2928  if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k'))
[135]2929  {
[182]2930    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing"
2931                  " NK record at offset 0x%.8X.", offset);
[180]2932    goto fail_locked;
[135]2933  }
[99]2934
[150]2935  ret_val->values = NULL;
2936  ret_val->subkeys = NULL;
[99]2937  ret_val->offset = offset;
[101]2938  ret_val->cell_size = cell_length;
2939
[99]2940  if(ret_val->cell_size > max_size)
2941    ret_val->cell_size = max_size & 0xFFFFFFF8;
2942  if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) 
[157]2943     || (strict && (ret_val->cell_size & 0x00000007) != 0))
[99]2944  {
[182]2945    regfi_log_add(REGFI_LOG_WARN, "A length check failed while"
2946                  " parsing NK record at offset 0x%.8X.", offset);
[180]2947    goto fail_locked;
[99]2948  }
2949
[101]2950  ret_val->magic[0] = nk_header[0x0];
2951  ret_val->magic[1] = nk_header[0x1];
[161]2952  ret_val->flags = SVAL(nk_header, 0x2);
[152]2953 
[161]2954  if((ret_val->flags & ~REGFI_NK_KNOWN_FLAGS) != 0)
[99]2955  {
[182]2956    regfi_log_add(REGFI_LOG_WARN, "Unknown key flags (0x%.4X) while"
2957                  " parsing NK record at offset 0x%.8X.", 
2958                  (ret_val->flags & ~REGFI_NK_KNOWN_FLAGS), offset);
[99]2959  }
[101]2960
[251]2961  ret_val->mtime = ((uint64_t)IVAL(nk_header, 0x8)) << 32;
2962  ret_val->mtime |= IVAL(nk_header, 0x4);
[116]2963  /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990
2964   * or later than Jan 1, 2290, we consider this a bad key.  This helps
2965   * weed out some false positives during deleted data recovery.
2966   */
2967  if(unalloc
[251]2968     && (ret_val->mtime < REGFI_MTIME_MIN
2969         || ret_val->mtime > REGFI_MTIME_MAX))
[180]2970  { goto fail_locked; }
[116]2971
[101]2972  ret_val->unknown1 = IVAL(nk_header, 0xC);
2973  ret_val->parent_off = IVAL(nk_header, 0x10);
2974  ret_val->num_subkeys = IVAL(nk_header, 0x14);
2975  ret_val->unknown2 = IVAL(nk_header, 0x18);
2976  ret_val->subkeys_off = IVAL(nk_header, 0x1C);
2977  ret_val->unknown3 = IVAL(nk_header, 0x20);
2978  ret_val->num_values = IVAL(nk_header, 0x24);
2979  ret_val->values_off = IVAL(nk_header, 0x28);
2980  ret_val->sk_off = IVAL(nk_header, 0x2C);
2981  ret_val->classname_off = IVAL(nk_header, 0x30);
[99]2982
[101]2983  ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34);
2984  ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38);
2985  ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C);
2986  ret_val->max_bytes_value = IVAL(nk_header, 0x40);
2987  ret_val->unk_index = IVAL(nk_header, 0x44);
[99]2988
[101]2989  ret_val->name_length = SVAL(nk_header, 0x48);
2990  ret_val->classname_length = SVAL(nk_header, 0x4A);
[206]2991  ret_val->name = NULL;
[99]2992
2993  if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size)
[101]2994  {
2995    if(strict)
2996    {
[182]2997      regfi_log_add(REGFI_LOG_ERROR, "Contents too large for cell"
2998                    " while parsing NK record at offset 0x%.8X.", offset);
[180]2999      goto fail_locked;
[101]3000    }
3001    else
3002      ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH;
3003  }
3004  else if (unalloc)
3005  { /* Truncate cell_size if it's much larger than the apparent total record length. */
3006    /* Round up to the next multiple of 8 */
3007    length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8;
3008    if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH)
3009      length+=8;
[99]3010
[101]3011    /* If cell_size is still greater, truncate. */
3012    if(length < ret_val->cell_size)
3013      ret_val->cell_size = length;
3014  }
3015
[206]3016  /* +1 to length in case we decided to use this directly as a string later */
3017  ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1);
3018  if(ret_val->name_raw == NULL)
[180]3019    goto fail_locked;
[99]3020
3021  /* Don't need to seek, should be at the right offset */
3022  length = ret_val->name_length;
[206]3023  if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0)
[99]3024     || length != ret_val->name_length)
3025  {
[182]3026    regfi_log_add(REGFI_LOG_ERROR, "Failed to read key name"
3027                  " while parsing NK record at offset 0x%.8X.", offset);
[180]3028    goto fail_locked;
[99]3029  }
3030
[186]3031  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_nk"))
[180]3032    goto fail;
3033
[126]3034  return ret_val;
[180]3035
3036 fail_locked:
[186]3037  regfi_unlock(file, &file->cb_lock, "regfi_parse_nk");
[180]3038 fail:
3039  talloc_free(ret_val);
3040  return NULL;
[126]3041}
3042
3043
[168]3044uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset, 
[206]3045                               uint16_t* name_length, uint32_t max_size, bool strict)
[126]3046{
[168]3047  uint8_t* ret_val = NULL;
3048  uint32_t length;
3049  uint32_t cell_length;
[126]3050  bool unalloc = false;
3051
[180]3052  if(*name_length <= 0 || offset == REGFI_OFFSET_NONE 
3053     || (offset & 0x00000007) != 0)
3054  { goto fail; }
3055
[186]3056  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_classname"))
[180]3057    goto fail;
3058
3059  if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc))
[131]3060  {
[182]3061    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
3062                  " while parsing class name at offset 0x%.8X.", offset);
[180]3063    goto fail_locked;
3064  }
3065 
3066  if((cell_length & 0x0000007) != 0)
3067  {
[182]3068    regfi_log_add(REGFI_LOG_ERROR, "Cell length not a multiple of 8"
3069                  " while parsing class name at offset 0x%.8X.", offset);
[180]3070    goto fail_locked;
3071  }
3072 
3073  if(cell_length > max_size)
3074  {
[182]3075    regfi_log_add(REGFI_LOG_WARN, "Cell stretches past hbin "
3076                  "boundary while parsing class name at offset 0x%.8X.",
3077                  offset);
[180]3078    if(strict)
3079      goto fail_locked;
3080    cell_length = max_size;
3081  }
3082 
3083  if((cell_length - 4) < *name_length)
3084  {
[182]3085    regfi_log_add(REGFI_LOG_WARN, "Class name is larger than"
3086                  " cell_length while parsing class name at offset"
3087                  " 0x%.8X.", offset);
[180]3088    if(strict)
3089      goto fail_locked;
3090    *name_length = cell_length - 4;
3091  }
3092 
3093  ret_val = talloc_array(NULL, uint8_t, *name_length);
3094  if(ret_val != NULL)
3095  {
3096    length = *name_length;
3097    if((regfi_read(file->cb, ret_val, &length) != 0)
3098       || length != *name_length)
[137]3099    {
[182]3100      regfi_log_add(REGFI_LOG_ERROR, "Could not read class name"
3101                    " while parsing class name at offset 0x%.8X.", offset);
[180]3102      goto fail_locked;
[137]3103    }
[180]3104  }
[126]3105
[186]3106  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_classname"))
[180]3107    goto fail;
[137]3108
[180]3109  return ret_val;
[131]3110
[180]3111 fail_locked:
[186]3112  regfi_unlock(file, &file->cb_lock, "regfi_parse_classname");
[180]3113 fail:
3114  talloc_free(ret_val);
3115  return NULL;
[99]3116}
3117
3118
[152]3119/******************************************************************************
3120*******************************************************************************/
[203]3121REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
[168]3122                             uint32_t max_size, bool strict)
[97]3123{
[203]3124  REGFI_VK* ret_val;
[168]3125  uint8_t vk_header[REGFI_VK_MIN_LENGTH];
3126  uint32_t raw_data_size, length, cell_length;
[101]3127  bool unalloc = false;
[97]3128
[203]3129  ret_val = talloc(NULL, REGFI_VK);
[180]3130  if(ret_val == NULL)
3131    goto fail;
3132
[186]3133  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_nk"))
[180]3134    goto fail;
3135
[178]3136  if(!regfi_parse_cell(file->cb, offset, vk_header, REGFI_VK_MIN_LENGTH,
[101]3137                       &cell_length, &unalloc))
[137]3138  {
[182]3139    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
3140                  " while parsing VK record at offset 0x%.8X.", offset);
[180]3141    goto fail_locked;
[137]3142  }
[111]3143
[101]3144  ret_val->offset = offset;
3145  ret_val->cell_size = cell_length;
[206]3146  ret_val->name = NULL;
3147  ret_val->name_raw = NULL;
[150]3148 
[101]3149  if(ret_val->cell_size > max_size)
3150    ret_val->cell_size = max_size & 0xFFFFFFF8;
3151  if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) 
[157]3152     || (ret_val->cell_size & 0x00000007) != 0)
[97]3153  {
[182]3154    regfi_log_add(REGFI_LOG_WARN, "Invalid cell size encountered"
3155                  " while parsing VK record at offset 0x%.8X.", offset);
[180]3156    goto fail_locked;
[101]3157  }
[97]3158
[101]3159  ret_val->magic[0] = vk_header[0x0];
3160  ret_val->magic[1] = vk_header[0x1];
3161  if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k'))
3162  {
[124]3163    /* XXX: This does not account for deleted keys under Win2K which
3164     *      often have this (and the name length) overwritten with
3165     *      0xFFFF.
3166     */
[182]3167    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch"
3168                  " while parsing VK record at offset 0x%.8X.", offset);
[180]3169    goto fail_locked;
[101]3170  }
3171
3172  ret_val->name_length = SVAL(vk_header, 0x2);
3173  raw_data_size = IVAL(vk_header, 0x4);
[135]3174  ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET;
[157]3175  /* The data is typically stored in the offset if the size <= 4,
3176   * in which case this flag is set.
3177   */
[135]3178  ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET);
[101]3179  ret_val->data_off = IVAL(vk_header, 0x8);
3180  ret_val->type = IVAL(vk_header, 0xC);
[162]3181  ret_val->flags = SVAL(vk_header, 0x10);
[101]3182  ret_val->unknown1 = SVAL(vk_header, 0x12);
3183
[162]3184  if(ret_val->name_length > 0)
[101]3185  {
[113]3186    if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size)
[101]3187    {
[182]3188      regfi_log_add(REGFI_LOG_WARN, "Name too long for remaining cell"
3189                    " space while parsing VK record at offset 0x%.8X.",
3190                    offset);
[101]3191      if(strict)
[180]3192        goto fail_locked;
[101]3193      else
[113]3194        ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4;
[101]3195    }
3196
3197    /* Round up to the next multiple of 8 */
[113]3198    cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8;
3199    if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4)
3200      cell_length+=8;
[101]3201
[206]3202    /* +1 to length in case we decided to use this directly as a string later */
3203    ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1);
3204    if(ret_val->name_raw == NULL)
[180]3205      goto fail_locked;
[113]3206
[101]3207    length = ret_val->name_length;
[206]3208    if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0)
[101]3209       || length != ret_val->name_length)
3210    {
[182]3211      regfi_log_add(REGFI_LOG_ERROR, "Could not read value name"
3212                    " while parsing VK record at offset 0x%.8X.", offset);
[180]3213      goto fail_locked;
[101]3214    }
3215  }
3216  else
[113]3217    cell_length = REGFI_VK_MIN_LENGTH + 4;
[101]3218
[186]3219  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_nk"))
[180]3220    goto fail;
3221
[101]3222  if(unalloc)
3223  {
3224    /* If cell_size is still greater, truncate. */
[113]3225    if(cell_length < ret_val->cell_size)
3226      ret_val->cell_size = cell_length;
[101]3227  }
3228
3229  return ret_val;
[180]3230 
3231 fail_locked:
[186]3232  regfi_unlock(file, &file->cb_lock, "regfi_parse_vk");
[180]3233 fail:
3234  talloc_free(ret_val);
3235  return NULL;
[97]3236}
[101]3237
3238
[152]3239/******************************************************************************
[157]3240 *
3241 ******************************************************************************/
[168]3242REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset,
3243                             uint32_t length, bool data_in_offset,
[157]3244                             bool strict)
[101]3245{
[151]3246  REGFI_BUFFER ret_val;
[168]3247  uint32_t cell_length, offset;
3248  int32_t max_size;
[101]3249  bool unalloc;
[151]3250 
[159]3251  /* Microsoft's documentation indicates that "available memory" is
[165]3252   * the limit on value sizes for the more recent registry format version.
3253   * This is not only annoying, but it's probably also incorrect, since clearly
3254   * value data sizes are limited to 2^31 (high bit used as a flag) and even
3255   * with big data records, the apparent max size is:
3256   *   16344 * 2^16 = 1071104040 (~1GB).
3257   *
3258   * We choose to limit it to 1M which was the limit in older versions and
3259   * should rarely be exceeded unless the file is corrupt or malicious.
3260   * For more info, see:
3261   *   http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
[159]3262   */
[160]3263  /* XXX: add way to skip this check at user discression. */
3264  if(length > REGFI_VK_MAX_DATA_LENGTH)
[159]3265  {
[182]3266    regfi_log_add(REGFI_LOG_WARN, "Value data size %d larger than "
3267                  "%d, truncating...", length, REGFI_VK_MAX_DATA_LENGTH);
[160]3268    length = REGFI_VK_MAX_DATA_LENGTH;
[159]3269  }
3270
[145]3271  if(data_in_offset)
[157]3272    return regfi_parse_little_data(file, voffset, length, strict);
3273  else
[101]3274  {
[157]3275    offset = voffset + REGFI_REGF_SIZE;
3276    max_size = regfi_calc_maxsize(file, offset);
3277    if(max_size < 0)
[137]3278    {
[182]3279      regfi_log_add(REGFI_LOG_WARN, "Could not find HBIN for data"
3280                    " at offset 0x%.8X.", offset);
[151]3281      goto fail;
[137]3282    }
[157]3283   
[186]3284    if(!regfi_lock(file, &file->cb_lock, "regfi_load_data"))
[180]3285      goto fail;
3286
[178]3287    if(!regfi_parse_cell(file->cb, offset, NULL, 0,
[101]3288                         &cell_length, &unalloc))
[137]3289    {
[182]3290      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3291                    " parsing data record at offset 0x%.8X.", offset);
[180]3292      goto fail_locked;
[137]3293    }
[111]3294
[186]3295    if(!regfi_unlock(file, &file->cb_lock, "regfi_load_data"))
[180]3296      goto fail;
3297
[157]3298    if((cell_length & 0x00000007) != 0)
[137]3299    {
[182]3300      regfi_log_add(REGFI_LOG_WARN, "Cell length not multiple of 8"
3301                    " while parsing data record at offset 0x%.8X.",
3302                    offset);
[151]3303      goto fail;
[137]3304    }
[101]3305
[131]3306    if(cell_length > max_size)
3307    {
[182]3308      regfi_log_add(REGFI_LOG_WARN, "Cell extends past HBIN boundary"
3309                    " while parsing data record at offset 0x%.8X.",
3310                    offset);
[157]3311      goto fail;
[131]3312    }
3313
[101]3314    if(cell_length - 4 < length)
3315    {
[155]3316      /* XXX: All big data records thus far have been 16 bytes long. 
3317       *      Should we check for this precise size instead of just
3318       *      relying upon the above check?
3319       */
[152]3320      if (file->major_version >= 1 && file->minor_version >= 5)
3321      {
3322        /* Attempt to parse a big data record */
[157]3323        return regfi_load_big_data(file, offset, length, cell_length, 
3324                                   NULL, strict);
[152]3325      }
[101]3326      else
[152]3327      {
[182]3328        regfi_log_add(REGFI_LOG_WARN, "Data length (0x%.8X) larger than"
3329                      " remaining cell length (0x%.8X)"
3330                      " while parsing data record at offset 0x%.8X.", 
3331                      length, cell_length - 4, offset);
[152]3332        if(strict)
3333          goto fail;
3334        else
3335          length = cell_length - 4;
3336      }
[101]3337    }
3338
[157]3339    ret_val = regfi_parse_data(file, offset, length, strict);
[101]3340  }
3341
3342  return ret_val;
[151]3343
[180]3344 fail_locked:
[186]3345  regfi_unlock(file, &file->cb_lock, "regfi_load_data");
[151]3346 fail:
3347  ret_val.buf = NULL;
3348  ret_val.len = 0;
3349  return ret_val;
[101]3350}
[110]3351
3352
[152]3353/******************************************************************************
[157]3354 * Parses the common case data records stored in a single cell.
3355 ******************************************************************************/
[168]3356REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset,
3357                              uint32_t length, bool strict)
[157]3358{
3359  REGFI_BUFFER ret_val;
[168]3360  uint32_t read_length;
[157]3361
3362  ret_val.buf = NULL;
3363  ret_val.len = 0;
3364 
[180]3365  if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL)
3366    goto fail;
3367  ret_val.len = length;
3368
[186]3369  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_data"))
[180]3370    goto fail;
3371
[178]3372  if(regfi_seek(file->cb, offset+4, SEEK_SET) == -1)
[157]3373  {
[182]3374    regfi_log_add(REGFI_LOG_WARN, "Could not seek while "
3375                  "reading data at offset 0x%.8X.", offset);
[180]3376    goto fail_locked;
[157]3377  }
3378 
3379  read_length = length;
[178]3380  if((regfi_read(file->cb, ret_val.buf, &read_length) != 0)
[157]3381     || read_length != length)
3382  {
[182]3383    regfi_log_add(REGFI_LOG_ERROR, "Could not read data block while"
3384                  " parsing data record at offset 0x%.8X.", offset);
[180]3385    goto fail_locked;
[157]3386  }
3387
[186]3388  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_data"))
[180]3389    goto fail;
3390
[157]3391  return ret_val;
[180]3392
3393 fail_locked:
[186]3394  regfi_unlock(file, &file->cb_lock, "regfi_parse_data");
[180]3395 fail:
3396  talloc_free(ret_val.buf);
3397  ret_val.buf = NULL;
3398  ret_val.buf = 0;
3399  return ret_val;
[157]3400}
3401
3402
3403
3404/******************************************************************************
3405 *
3406 ******************************************************************************/
[168]3407REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset,
3408                                     uint32_t length, bool strict)
[157]3409{
[173]3410  uint8_t i;
[157]3411  REGFI_BUFFER ret_val;
3412
3413  ret_val.buf = NULL;
3414  ret_val.len = 0;
3415
3416  if(length > 4)
3417  {
[182]3418    regfi_log_add(REGFI_LOG_ERROR, "Data in offset but length > 4"
3419                  " while parsing data record. (voffset=0x%.8X, length=%d)",
3420                  voffset, length);
[157]3421    return ret_val;
3422  }
3423
[168]3424  if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL)
[157]3425    return ret_val;
3426  ret_val.len = length;
3427 
3428  for(i = 0; i < length; i++)
[168]3429    ret_val.buf[i] = (uint8_t)((voffset >> i*8) & 0xFF);
[157]3430
3431  return ret_val;
3432}
3433
3434/******************************************************************************
[152]3435*******************************************************************************/
[168]3436REGFI_BUFFER regfi_parse_big_data_header(REGFI_FILE* file, uint32_t offset, 
3437                                         uint32_t max_size, bool strict)
[152]3438{
3439  REGFI_BUFFER ret_val;
[168]3440  uint32_t cell_length;
[152]3441  bool unalloc;
[157]3442
3443  /* XXX: do something with unalloc? */
[168]3444  ret_val.buf = (uint8_t*)talloc_array(NULL, uint8_t, REGFI_BIG_DATA_MIN_LENGTH);
[157]3445  if(ret_val.buf == NULL)
[152]3446    goto fail;
3447
[157]3448  if(REGFI_BIG_DATA_MIN_LENGTH > max_size)
3449  {
[182]3450    regfi_log_add(REGFI_LOG_WARN, "Big data header exceeded max_size "
3451                  "while parsing big data header at offset 0x%.8X.",offset);
[157]3452    goto fail;
3453  }
3454
[186]3455  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_header"))
[180]3456    goto fail;
3457
3458
[178]3459  if(!regfi_parse_cell(file->cb, offset, ret_val.buf, REGFI_BIG_DATA_MIN_LENGTH,
[152]3460                       &cell_length, &unalloc))
3461  {
[182]3462    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3463                  " parsing big data header at offset 0x%.8X.", offset);
[180]3464    goto fail_locked;
[152]3465  }
[157]3466
[186]3467  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_header"))
[180]3468    goto fail;
3469
[157]3470  if((ret_val.buf[0] != 'd') || (ret_val.buf[1] != 'b'))
[152]3471  {
[182]3472    regfi_log_add(REGFI_LOG_WARN, "Unknown magic number"
3473                  " (0x%.2X, 0x%.2X) encountered while parsing"
3474                  " big data header at offset 0x%.8X.", 
3475                  ret_val.buf[0], ret_val.buf[1], offset);
[152]3476    goto fail;
3477  }
3478
[157]3479  ret_val.len = REGFI_BIG_DATA_MIN_LENGTH;
3480  return ret_val;
3481
[180]3482 fail_locked:
[186]3483  regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_header");
[157]3484 fail:
[180]3485  talloc_free(ret_val.buf);
3486  ret_val.buf = NULL;
[157]3487  ret_val.len = 0;
3488  return ret_val;
3489}
3490
3491
3492
3493/******************************************************************************
3494 *
3495 ******************************************************************************/
[168]3496uint32_t* regfi_parse_big_data_indirect(REGFI_FILE* file, uint32_t offset,
3497                                      uint16_t num_chunks, bool strict)
[157]3498{
[168]3499  uint32_t* ret_val;
3500  uint32_t indirect_length;
3501  int32_t max_size;
3502  uint16_t i;
[157]3503  bool unalloc;
3504
3505  /* XXX: do something with unalloc? */
3506  max_size = regfi_calc_maxsize(file, offset);
[168]3507  if((max_size < 0) || (num_chunks*sizeof(uint32_t) + 4 > max_size))
[157]3508    return NULL;
3509
[168]3510  ret_val = (uint32_t*)talloc_array(NULL, uint32_t, num_chunks);
[157]3511  if(ret_val == NULL)
[152]3512    goto fail;
3513
[186]3514  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_indirect"))
[180]3515    goto fail;
3516
[178]3517  if(!regfi_parse_cell(file->cb, offset, (uint8_t*)ret_val,
[168]3518                       num_chunks*sizeof(uint32_t),
[152]3519                       &indirect_length, &unalloc))
3520  {
[182]3521    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3522                  " parsing big data indirect record at offset 0x%.8X.", 
3523                  offset);
[180]3524    goto fail_locked;
[152]3525  }
[157]3526
[186]3527  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_indirect"))
[180]3528    goto fail;
3529
[157]3530  /* Convert pointers to proper endianess, verify they are aligned. */
3531  for(i=0; i<num_chunks; i++)
[152]3532  {
[168]3533    ret_val[i] = IVAL(ret_val, i*sizeof(uint32_t));
[157]3534    if((ret_val[i] & 0x00000007) != 0)
3535      goto fail;
[152]3536  }
[157]3537 
3538  return ret_val;
[152]3539
[180]3540 fail_locked:
[186]3541  regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_indirect");
[157]3542 fail:
[180]3543  talloc_free(ret_val);
[157]3544  return NULL;
3545}
3546
3547
3548/******************************************************************************
3549 * Arguments:
3550 *  file       --
3551 *  offsets    -- list of virtual offsets.
3552 *  num_chunks --
3553 *  strict     --
3554 *
3555 * Returns:
3556 *  A range_list with physical offsets and complete lengths
3557 *  (including cell headers) of associated cells. 
3558 *  No data in range_list elements.
3559 ******************************************************************************/
[168]3560range_list* regfi_parse_big_data_cells(REGFI_FILE* file, uint32_t* offsets,
3561                                       uint16_t num_chunks, bool strict)
[157]3562{
[168]3563  uint32_t cell_length, chunk_offset;
[157]3564  range_list* ret_val;
[168]3565  uint16_t i;
[157]3566  bool unalloc;
3567 
3568  /* XXX: do something with unalloc? */
3569  ret_val = range_list_new();
3570  if(ret_val == NULL)
3571    goto fail;
3572 
[166]3573  for(i=0; i<num_chunks; i++)
[152]3574  {
[186]3575    if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_cells"))
[180]3576      goto fail;
3577
[157]3578    chunk_offset = offsets[i]+REGFI_REGF_SIZE;
[178]3579    if(!regfi_parse_cell(file->cb, chunk_offset, NULL, 0,
[157]3580                         &cell_length, &unalloc))
[152]3581    {
[182]3582      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3583                    " parsing big data chunk at offset 0x%.8X.", 
3584                    chunk_offset);
[180]3585      goto fail_locked;
[152]3586    }
3587
[186]3588    if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_cells"))
[180]3589      goto fail;
3590
[157]3591    if(!range_list_add(ret_val, chunk_offset, cell_length, NULL))
3592      goto fail;
3593  }
3594
3595  return ret_val;
3596
[180]3597 fail_locked:
[186]3598  regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_cells");
[157]3599 fail:
3600  if(ret_val != NULL)
3601    range_list_free(ret_val);
3602  return NULL;
3603}
3604
3605
3606/******************************************************************************
3607*******************************************************************************/
3608REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, 
[168]3609                                 uint32_t offset, uint32_t data_length, 
3610                                 uint32_t cell_length, range_list* used_ranges,
[157]3611                                 bool strict)
3612{
3613  REGFI_BUFFER ret_val;
[168]3614  uint16_t num_chunks, i;
3615  uint32_t read_length, data_left, tmp_len, indirect_offset;
3616  uint32_t* indirect_ptrs = NULL;
[157]3617  REGFI_BUFFER bd_header;
3618  range_list* bd_cells = NULL;
3619  const range_list_element* cell_info;
3620
3621  ret_val.buf = NULL;
3622
3623  /* XXX: Add better error/warning messages */
3624
3625  bd_header = regfi_parse_big_data_header(file, offset, cell_length, strict);
3626  if(bd_header.buf == NULL)
3627    goto fail;
3628
3629  /* Keep track of used space for use by reglookup-recover */
3630  if(used_ranges != NULL)
3631    if(!range_list_add(used_ranges, offset, cell_length, NULL))
3632      goto fail;
3633
3634  num_chunks = SVAL(bd_header.buf, 0x2);
3635  indirect_offset = IVAL(bd_header.buf, 0x4) + REGFI_REGF_SIZE;
3636  talloc_free(bd_header.buf);
3637
3638  indirect_ptrs = regfi_parse_big_data_indirect(file, indirect_offset,
3639                                                num_chunks, strict);
3640  if(indirect_ptrs == NULL)
3641    goto fail;
3642
3643  if(used_ranges != NULL)
3644    if(!range_list_add(used_ranges, indirect_offset, num_chunks*4+4, NULL))
3645      goto fail;
3646 
3647  if((ret_val.buf = talloc_array(NULL, uint8_t, data_length)) == NULL)
3648    goto fail;
3649  data_left = data_length;
3650
3651  bd_cells = regfi_parse_big_data_cells(file, indirect_ptrs, num_chunks, strict);
3652  if(bd_cells == NULL)
3653    goto fail;
3654
3655  talloc_free(indirect_ptrs);
3656  indirect_ptrs = NULL;
3657 
3658  for(i=0; (i<num_chunks) && (data_left>0); i++)
3659  {
3660    cell_info = range_list_get(bd_cells, i);
3661    if(cell_info == NULL)
3662      goto fail;
3663
3664    /* XXX: This should be "cell_info->length-4" to account for the 4 byte cell
[154]3665     *      length.  However, it has been observed that some (all?) chunks
3666     *      have an additional 4 bytes of 0 at the end of their cells that
3667     *      isn't part of the data, so we're trimming that off too.
[157]3668     *      Perhaps it's just an 8 byte alignment requirement...
[154]3669     */
[157]3670    if(cell_info->length - 8 >= data_left)
3671    {
3672      if(i+1 != num_chunks)
3673      {
[182]3674        regfi_log_add(REGFI_LOG_WARN, "Left over chunks detected "
3675                      "while constructing big data at offset 0x%.8X "
3676                      "(chunk offset 0x%.8X).", offset, cell_info->offset);
[157]3677      }
[152]3678      read_length = data_left;
[157]3679    }
[152]3680    else
[157]3681      read_length = cell_info->length - 8;
[152]3682
[157]3683
3684    if(read_length > regfi_calc_maxsize(file, cell_info->offset))
3685    {
[182]3686      regfi_log_add(REGFI_LOG_WARN, "A chunk exceeded the maxsize "
3687                    "while constructing big data at offset 0x%.8X "
3688                    "(chunk offset 0x%.8X).", offset, cell_info->offset);
[157]3689      goto fail;
3690    }
3691
[186]3692    if(!regfi_lock(file, &file->cb_lock, "regfi_load_big_data"))
[180]3693      goto fail;
3694
[178]3695    if(regfi_seek(file->cb, cell_info->offset+sizeof(uint32_t), SEEK_SET) == -1)
[157]3696    {
[182]3697      regfi_log_add(REGFI_LOG_WARN, "Could not seek to chunk while "
3698                    "constructing big data at offset 0x%.8X "
3699                    "(chunk offset 0x%.8X).", offset, cell_info->offset);
[180]3700      goto fail_locked;
[157]3701    }
3702
3703    tmp_len = read_length;
[178]3704    if(regfi_read(file->cb, ret_val.buf+(data_length-data_left), 
[157]3705                  &read_length) != 0 || (read_length != tmp_len))
[152]3706    {
[182]3707      regfi_log_add(REGFI_LOG_WARN, "Could not read data chunk while"
3708                    " constructing big data at offset 0x%.8X"
3709                    " (chunk offset 0x%.8X).", offset, cell_info->offset);
[180]3710      goto fail_locked;
[152]3711    }
3712
[186]3713    if(!regfi_unlock(file, &file->cb_lock, "regfi_load_big_data"))
[180]3714      goto fail;
3715
[157]3716    if(used_ranges != NULL)
3717      if(!range_list_add(used_ranges, cell_info->offset,cell_info->length,NULL))
3718        goto fail;
3719
[152]3720    data_left -= read_length;
3721  }
[157]3722  range_list_free(bd_cells);
3723
[152]3724  ret_val.len = data_length-data_left;
3725  return ret_val;
3726
[180]3727 fail_locked:
[186]3728  regfi_unlock(file, &file->cb_lock, "regfi_load_big_data");
[152]3729 fail:
[180]3730  talloc_free(ret_val.buf);
3731  talloc_free(indirect_ptrs);
[157]3732  if(bd_cells != NULL)
3733    range_list_free(bd_cells);
[152]3734  ret_val.buf = NULL;
3735  ret_val.len = 0;
3736  return ret_val;
3737}
3738
3739
[135]3740range_list* regfi_parse_unalloc_cells(REGFI_FILE* file)
[110]3741{
3742  range_list* ret_val;
[135]3743  REGFI_HBIN* hbin;
[110]3744  const range_list_element* hbins_elem;
[168]3745  uint32_t i, num_hbins, curr_off, cell_len;
[110]3746  bool is_unalloc;
3747
3748  ret_val = range_list_new();
3749  if(ret_val == NULL)
3750    return NULL;
3751
[186]3752  if(!regfi_read_lock(file, &file->hbins_lock, "regfi_parse_unalloc_cells"))
[180]3753  {
3754    range_list_free(ret_val);
3755    return NULL;
3756  }
3757
[110]3758  num_hbins = range_list_size(file->hbins);
3759  for(i=0; i<num_hbins; i++)
3760  {
3761    hbins_elem = range_list_get(file->hbins, i);
3762    if(hbins_elem == NULL)
3763      break;
[135]3764    hbin = (REGFI_HBIN*)hbins_elem->data;
[110]3765
[135]3766    curr_off = REGFI_HBIN_HEADER_SIZE;
[110]3767    while(curr_off < hbin->block_size)
3768    {
[186]3769      if(!regfi_lock(file, &file->cb_lock, "regfi_parse_unalloc_cells"))
[180]3770        break;
3771
[178]3772      if(!regfi_parse_cell(file->cb, hbin->file_off+curr_off, NULL, 0,
[110]3773                           &cell_len, &is_unalloc))
[180]3774      {
[186]3775        regfi_unlock(file, &file->cb_lock, "regfi_parse_unalloc_cells");
[110]3776        break;
[180]3777      }
3778
[186]3779      if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_unalloc_cells"))
[180]3780        break;
3781
[157]3782      if((cell_len == 0) || ((cell_len & 0x00000007) != 0))
[140]3783      {
[182]3784        regfi_log_add(REGFI_LOG_ERROR, "Bad cell length encountered"
3785                      " while parsing unallocated cells at offset 0x%.8X.",
3786                      hbin->file_off+curr_off);
[110]3787        break;
[140]3788      }
3789
[110]3790      /* for some reason the record_size of the last record in
3791         an hbin block can extend past the end of the block
3792         even though the record fits within the remaining
3793         space....aaarrrgggghhhhhh */ 
3794      if(curr_off + cell_len >= hbin->block_size)
3795        cell_len = hbin->block_size - curr_off;
3796     
3797      if(is_unalloc)
3798        range_list_add(ret_val, hbin->file_off+curr_off, 
3799                       cell_len, NULL);
3800     
3801      curr_off = curr_off+cell_len;
3802    }
3803  }
3804
[186]3805  if(!regfi_rw_unlock(file, &file->hbins_lock, "regfi_parse_unalloc_cells"))
[180]3806  {
3807    range_list_free(ret_val);
3808    return NULL;
3809  }
3810
[110]3811  return ret_val;
3812}
[168]3813
3814
3815/* From lib/time.c */
3816
3817/****************************************************************************
[251]3818 Returns an 8 byte filetime from a time_t
[168]3819 This takes real GMT as input and converts to kludge-GMT
3820****************************************************************************/
[251]3821REGFI_NTTIME regfi_unix2nt_time(time_t t)
[168]3822{
3823  double d;
[251]3824
3825  if (t==0)
3826    return 0L;
[168]3827 
3828  if (t == TIME_T_MAX) 
[251]3829    return 0x7fffffffffffffffL;
[168]3830 
3831  if (t == -1) 
[251]3832    return 0xffffffffffffffffL;
[168]3833 
3834  /* this converts GMT to kludge-GMT */
3835  /* XXX: This was removed due to difficult dependency requirements. 
3836   *      So far, times appear to be correct without this adjustment, but
3837   *      that may be proven wrong with adequate testing.
3838   */
3839  /* t -= TimeDiff(t) - get_serverzone(); */
3840 
[251]3841  d = (double)(t) + REGFI_TIME_FIXUP;
[168]3842  d *= 1.0e7;
[251]3843  /*
3844  nt->high = (uint32_t)(d * (1.0/c));
3845  nt->low  = (uint32_t)(d - ((double)nt->high) * c);
3846  */
3847
3848  return (REGFI_NTTIME) d;
[168]3849}
3850
3851
3852/****************************************************************************
3853 Interpret an 8 byte "filetime" structure to a time_t
3854 It's originally in "100ns units since jan 1st 1601"
3855
3856 An 8 byte value of 0xffffffffffffffff will be returned as (time_t)0.
3857
3858 It appears to be kludge-GMT (at least for file listings). This means
3859 its the GMT you get by taking a localtime and adding the
3860 serverzone. This is NOT the same as GMT in some cases. This routine
3861 converts this to real GMT.
3862****************************************************************************/
[251]3863double regfi_nt2unix_time(REGFI_NTTIME nt)
[168]3864{
[219]3865  double ret_val;
[168]3866 
[251]3867  if (nt == 0 || nt == 0xffffffffffffffffL)
3868    return 0;
[168]3869 
[251]3870  ret_val = (double)(nt) * 1.0e-7;
[168]3871 
3872  /* now adjust by 369 years to make the secs since 1970 */
[251]3873  ret_val -= REGFI_TIME_FIXUP;
[168]3874 
3875  /* this takes us from kludge-GMT to real GMT */
3876  /* XXX: This was removed due to difficult dependency requirements. 
3877   *      So far, times appear to be correct without this adjustment, but
3878   *      that may be proven wrong with adequate testing.
3879   */
3880  /*
3881    ret -= get_serverzone();
3882    ret += LocTimeDiff(ret);
3883  */
3884
[219]3885  return ret_val;
[168]3886}
3887
3888/* End of stuff from lib/time.c */
Note: See TracBrowser for help on using the repository browser.