source: trunk/lib/regfi.c @ 262

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

changed regfi_conv_charset to handle memory allocation
tweaked test cases
corrected some documentation

  • Property svn:keywords set to Id
File size: 107.2 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 262 2011-06-17 17:51:31Z 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   */
[262]1188  REGFI_BUFFER tmp_buf;
[172]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  {
[262]1202    tmp_buf = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
1203                                 regfi_encoding_int2str(output_encoding),
1204                                 vk->name_raw, vk->name_length);
1205    if(tmp_buf.buf == NULL)
[162]1206    {
[182]1207      regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
[206]1208                        " value name to encoding %s.  Error message: %s",
[162]1209                        regfi_encoding_int2str(output_encoding), 
[262]1210                        strerror(errno));
[206]1211      vk->name = NULL;
[162]1212    }
[262]1213    else
1214    {
1215      vk->name = (char*)tmp_buf.buf;
1216      talloc_reparent(NULL, vk, vk->name);
1217    }
[162]1218  }
[172]1219}
[162]1220
[172]1221
1222/******************************************************************************
1223 ******************************************************************************/
[203]1224REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
[206]1225                           REGFI_ENCODING output_encoding, bool strict)
[172]1226{
[203]1227  REGFI_VK* ret_val = NULL;
[172]1228  int32_t max_size;
1229
1230  max_size = regfi_calc_maxsize(file, offset);
1231  if(max_size < 0)
1232    return NULL;
1233 
1234  ret_val = regfi_parse_vk(file, offset, max_size, strict);
1235  if(ret_val == NULL)
1236    return NULL;
1237
1238  regfi_interpret_valuename(file, ret_val, output_encoding, strict);
1239
[103]1240  return ret_val;
[30]1241}
1242
1243
[145]1244/******************************************************************************
1245 * If !strict, the list may contain NULLs, VK records may point to NULL.
1246 ******************************************************************************/
[168]1247REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
1248                                       uint32_t num_values, uint32_t max_size,
[145]1249                                       bool strict)
1250{
[168]1251  uint32_t usable_num_values;
[30]1252
[168]1253  if((num_values+1) * sizeof(uint32_t) > max_size)
[145]1254  {
[182]1255    regfi_log_add(REGFI_LOG_WARN, "Number of values indicated by"
[145]1256                      " parent key (%d) would cause cell to straddle HBIN"
1257                      " boundary while loading value list at offset"
1258                      " 0x%.8X.", num_values, offset);
1259    if(strict)
1260      return NULL;
[168]1261    usable_num_values = max_size/sizeof(uint32_t) - sizeof(uint32_t);
[145]1262  }
1263  else
1264    usable_num_values = num_values;
1265
1266  return regfi_parse_valuelist(file, offset, usable_num_values, strict);
1267}
1268
1269
[206]1270/* XXX: should give this boolean return type to indicate errors */
[203]1271void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, 
[161]1272                             REGFI_ENCODING output_encoding, bool strict)
[30]1273{
[165]1274  /* XXX: Registry key names are supposedly limited to 255 characters according to:
1275   *      http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
1276   *      Might want to emit a warning if this is exceeded. 
1277   *      It is expected that "characters" could be variable width.
1278   *      Also, it may be useful to use this information to limit false positives
1279   *      when recovering deleted NK records.
1280   */
[262]1281  REGFI_BUFFER tmp_buf;
[172]1282  REGFI_ENCODING from_encoding = (nk->flags & REGFI_NK_FLAG_ASCIINAME) 
[161]1283    ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE;
[256]1284
1285  if(nk->name_length == 0)
1286    return; 
1287
[161]1288  if(from_encoding == output_encoding)
1289  {
[206]1290    nk->name_raw[nk->name_length] = '\0';
1291    nk->name = (char*)nk->name_raw;
[161]1292  }
1293  else
1294  {
[262]1295    tmp_buf = regfi_conv_charset(regfi_encoding_int2str(from_encoding),
1296                                 regfi_encoding_int2str(output_encoding),
1297                                 nk->name_raw, nk->name_length);
1298    if(tmp_buf.buf == NULL)
[161]1299    {
[182]1300      regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
[262]1301                    " key name to encoding %s.  Error message: %s",
1302                    regfi_encoding_int2str(output_encoding), 
1303                    strerror(errno));
[206]1304      nk->name = NULL;
[161]1305    }
[262]1306    else
1307    {
1308      nk->name = (char*)tmp_buf.buf;
1309      talloc_reparent(NULL, nk, nk->name);
1310    }
[161]1311  }
[172]1312}
[161]1313
1314
[172]1315/******************************************************************************
1316 *
1317 ******************************************************************************/
[203]1318REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset,
[206]1319                         REGFI_ENCODING output_encoding, bool strict)
[172]1320{
[203]1321  REGFI_NK* nk;
[172]1322  uint32_t off;
1323  int32_t max_size;
1324
[250]1325  if(file->nk_cache != NULL)
1326  {
1327    /* First, check to see if we have this key in our cache */
1328    if(!regfi_lock(file, &file->mem_lock, "regfi_load_nk"))
1329      return NULL;
1330    regfi_lock(file, &file->nk_lock, "regfi_load_nk");
1331   
1332    nk = (REGFI_NK*)lru_cache_find(file->nk_cache, &offset, 4);
1333    if(nk != NULL)
1334      nk = talloc_reference(NULL, nk);
1335
1336    regfi_unlock(file, &file->nk_lock, "regfi_load_nk");
1337    regfi_unlock(file, &file->mem_lock, "regfi_load_nk");
1338    if(nk != NULL)
1339      return nk;
1340  }
1341
1342  /* Not cached currently, proceed with loading it */
[172]1343  max_size = regfi_calc_maxsize(file, offset);
1344  if (max_size < 0) 
1345    return NULL;
1346
1347  /* get the initial nk record */
1348  if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL)
1349  {
[182]1350    regfi_log_add(REGFI_LOG_ERROR, "Could not load NK record at"
1351                  " offset 0x%.8X.", offset);
[172]1352    return NULL;
1353  }
1354
1355  regfi_interpret_keyname(file, nk, output_encoding, strict);
1356
[146]1357  /* get value list */
[135]1358  if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) 
[32]1359  {
[157]1360    off = nk->values_off + REGFI_REGF_SIZE;
1361    max_size = regfi_calc_maxsize(file, off);
1362    if(max_size < 0)
[32]1363    {
[105]1364      if(strict)
[32]1365      {
[184]1366        talloc_free(nk);
[99]1367        return NULL;
[31]1368      }
[105]1369      else
1370        nk->values = NULL;
[31]1371    }
[105]1372    else
[103]1373    {
[157]1374      nk->values = regfi_load_valuelist(file, off, nk->num_values, 
1375                                        max_size, true);
[145]1376      if(nk->values == NULL)
[105]1377      {
[182]1378        regfi_log_add(REGFI_LOG_WARN, "Could not load value list"
1379                      " for NK record at offset 0x%.8X.", offset);
[145]1380        if(strict)
1381        {
[184]1382          talloc_free(nk);
[145]1383          return NULL;
1384        }
[105]1385      }
[223]1386      talloc_reparent(NULL, nk, nk->values);
[103]1387    }
[31]1388  }
[105]1389
[146]1390  /* now get subkey list */
[135]1391  if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) 
[32]1392  {
[157]1393    off = nk->subkeys_off + REGFI_REGF_SIZE;
1394    max_size = regfi_calc_maxsize(file, off);
1395    if(max_size < 0) 
[32]1396    {
[105]1397      if(strict)
[32]1398      {
[184]1399        talloc_free(nk);
[99]1400        return NULL;
[31]1401      }
[105]1402      else
1403        nk->subkeys = NULL;
[31]1404    }
[105]1405    else
[104]1406    {
[134]1407      nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys,
[157]1408                                          max_size, true);
[134]1409
[105]1410      if(nk->subkeys == NULL)
1411      {
[182]1412        regfi_log_add(REGFI_LOG_WARN, "Could not load subkey list"
1413                      " while parsing NK record at offset 0x%.8X.", offset);
[105]1414        nk->num_subkeys = 0;
1415      }
[223]1416      talloc_reparent(NULL, nk, nk->subkeys);
[104]1417    }
[31]1418  }
[30]1419
[250]1420  if(file->nk_cache != NULL)
1421  {
1422    /* All is well, so let us cache this key for later */
1423    if(!regfi_lock(file, &file->mem_lock, "regfi_load_nk"))
1424      return NULL;
1425    regfi_lock(file, &file->nk_lock, "regfi_load_nk");
1426   
1427    lru_cache_update(file->nk_cache, &offset, 4, nk);
1428   
1429    regfi_unlock(file, &file->nk_lock, "regfi_load_nk");
1430    regfi_unlock(file, &file->mem_lock, "regfi_load_nk");
1431  }
1432
[99]1433  return nk;
[30]1434}
1435
[32]1436
[102]1437/******************************************************************************
1438 ******************************************************************************/
[203]1439const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset, bool strict)
[146]1440{
[203]1441  REGFI_SK* ret_val = NULL;
[168]1442  int32_t max_size;
[147]1443  void* failure_ptr = NULL;
1444 
[184]1445  max_size = regfi_calc_maxsize(file, offset);
1446  if(max_size < 0)
1447    return NULL;
1448
1449  if(file->sk_cache == NULL)
1450    return regfi_parse_sk(file, offset, max_size, strict);
1451
[250]1452  if(!regfi_lock(file, &file->mem_lock, "regfi_load_sk"))
[180]1453    return NULL;
[250]1454  regfi_lock(file, &file->sk_lock, "regfi_load_sk");
[180]1455
[146]1456  /* First look if we have already parsed it */
[203]1457  ret_val = (REGFI_SK*)lru_cache_find(file->sk_cache, &offset, 4);
[146]1458
1459  /* Bail out if we have previously cached a parse failure at this offset. */
1460  if(ret_val == (void*)REGFI_OFFSET_NONE)
[250]1461  {
1462    ret_val = NULL;
1463    goto unlock;
1464  }
[146]1465
1466  if(ret_val == NULL)
1467  {
[157]1468    ret_val = regfi_parse_sk(file, offset, max_size, strict);
[146]1469    if(ret_val == NULL)
1470    { /* Cache the parse failure and bail out. */
[147]1471      failure_ptr = talloc(NULL, uint32_t);
1472      if(failure_ptr == NULL)
[250]1473        goto unlock;
1474
[147]1475      *(uint32_t*)failure_ptr = REGFI_OFFSET_NONE;
1476      lru_cache_update(file->sk_cache, &offset, 4, failure_ptr);
[184]1477
1478      /* Let the cache be the only owner of this */
1479      talloc_unlink(NULL, failure_ptr);
[146]1480    }
1481  }
[253]1482  else
1483    ret_val = talloc_reference(NULL, ret_val);
[146]1484
[250]1485 unlock:
1486  regfi_unlock(file, &file->sk_lock, "regfi_load_sk");
1487  regfi_unlock(file, &file->mem_lock, "regfi_load_sk");
[180]1488
[146]1489  return ret_val;
1490}
1491
1492
1493
1494/******************************************************************************
1495 ******************************************************************************/
[203]1496REGFI_NK* regfi_find_root_nk(REGFI_FILE* file, const REGFI_HBIN* hbin, 
[206]1497                             REGFI_ENCODING output_encoding)
[30]1498{
[203]1499  REGFI_NK* nk = NULL;
[168]1500  uint32_t cell_length;
1501  uint32_t cur_offset = hbin->file_off+REGFI_HBIN_HEADER_SIZE;
1502  uint32_t hbin_end = hbin->file_off+hbin->block_size;
[158]1503  bool unalloc;
[30]1504
[158]1505  while(cur_offset < hbin_end)
[32]1506  {
[180]1507
[186]1508    if(!regfi_lock(file, &file->cb_lock, "regfi_find_root_nk"))
[180]1509      return NULL;
1510
[178]1511    if(!regfi_parse_cell(file->cb, cur_offset, NULL, 0, &cell_length, &unalloc))
[158]1512    {
[182]1513      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell at offset"
1514                    " 0x%.8X while searching for root key.", cur_offset);
[255]1515      goto error_locked;
[158]1516    }
[180]1517
[186]1518    if(!regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk"))
[180]1519      return NULL;
1520
[158]1521    if(!unalloc)
[102]1522    {
[161]1523      nk = regfi_load_key(file, cur_offset, output_encoding, true);
[102]1524      if(nk != NULL)
1525      {
[161]1526        if(nk->flags & REGFI_NK_FLAG_ROOT)
[158]1527          return nk;
[102]1528      }
[31]1529    }
[30]1530
[158]1531    cur_offset += cell_length;
[31]1532  }
[32]1533
[158]1534  return NULL;
[255]1535
1536 error_locked:
1537  regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk");
1538  return NULL;
[30]1539}
1540
1541
[178]1542
[166]1543/******************************************************************************
1544 ******************************************************************************/
[206]1545REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding)
[30]1546{
[166]1547  REGFI_FILE* ret_val;
[178]1548  REGFI_RAW_FILE* file_cb = talloc(NULL, REGFI_RAW_FILE);
1549  if(file_cb == NULL) 
[31]1550    return NULL;
[166]1551
[178]1552  file_cb->state = (void*)talloc(file_cb, int);
1553  if(file_cb->state == NULL)
1554    goto fail;
1555  *(int*)file_cb->state = fd;
1556 
1557  file_cb->cur_off = 0;
1558  file_cb->size = 0;
1559  file_cb->read = &regfi_raw_read;
1560  file_cb->seek = &regfi_raw_seek;
1561 
[206]1562  ret_val = regfi_alloc_cb(file_cb, output_encoding);
[166]1563  if(ret_val == NULL)
[178]1564    goto fail;
[166]1565
[178]1566  /* In this case, we want file_cb to be freed when ret_val is */
[223]1567  talloc_reparent(NULL, ret_val, file_cb);
[166]1568  return ret_val;
[178]1569
1570 fail:
1571    talloc_free(file_cb);
1572    return NULL;
[166]1573}
1574
1575
[186]1576/******************************************************************************
1577 ******************************************************************************/
[223]1578static int regfi_free_cb(void* f)
[186]1579{
1580  REGFI_FILE* file = (REGFI_FILE*)f;
[178]1581
[186]1582  pthread_mutex_destroy(&file->cb_lock);
1583  pthread_rwlock_destroy(&file->hbins_lock);
1584  pthread_mutex_destroy(&file->sk_lock);
[250]1585  pthread_mutex_destroy(&file->nk_lock);
1586  pthread_mutex_destroy(&file->mem_lock);
[186]1587
1588  return 0;
1589}
1590
1591
1592/******************************************************************************
1593 ******************************************************************************/
[206]1594REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb, 
1595                           REGFI_ENCODING output_encoding)
[166]1596{
1597  REGFI_FILE* rb;
1598  REGFI_HBIN* hbin = NULL;
[178]1599  uint32_t hbin_off, cache_secret;
[226]1600  int64_t file_length;
[166]1601  bool rla;
1602
[178]1603  /* Determine file length.  Must be at least big enough for the header
1604   * and one hbin.
[137]1605   */
[226]1606  file_length = regfi_seek(file_cb, 0, SEEK_END);
[137]1607  if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC)
[182]1608  {
1609    regfi_log_add(REGFI_LOG_ERROR, "File length (%d) too short to contain a"
1610                  " header and at least one HBIN.", file_length);
[137]1611    return NULL;
[182]1612  }
[226]1613  regfi_seek(file_cb, 0, SEEK_SET);
[137]1614
[206]1615  if(output_encoding != REGFI_ENCODING_UTF8
1616     && output_encoding != REGFI_ENCODING_ASCII)
1617  { 
1618    regfi_log_add(REGFI_LOG_ERROR, "Invalid output_encoding supplied"
1619                  " in creation of regfi iterator.");
1620    return NULL;
1621  }
1622
[166]1623  /* Read file header */
[203]1624  if ((rb = regfi_parse_regf(file_cb, false)) == NULL)
[97]1625  {
[182]1626    regfi_log_add(REGFI_LOG_ERROR, "Failed to read REGF block.");
[31]1627    return NULL;
1628  }
[203]1629  rb->file_length = file_length;
[178]1630  rb->cb = file_cb;
[206]1631  rb->string_encoding = output_encoding;
[137]1632
[186]1633  if(pthread_mutex_init(&rb->cb_lock, NULL) != 0)
[182]1634  {
1635    regfi_log_add(REGFI_LOG_ERROR, "Failed to create cb_lock mutex.");
[180]1636    goto fail;
[182]1637  }
[180]1638
[186]1639  if(pthread_rwlock_init(&rb->hbins_lock, NULL) != 0)
[182]1640  {
1641    regfi_log_add(REGFI_LOG_ERROR, "Failed to create hbins_lock rwlock.");
[180]1642    goto fail;
[182]1643  }
[180]1644
[186]1645  if(pthread_mutex_init(&rb->sk_lock, NULL) != 0)
[182]1646  {
1647    regfi_log_add(REGFI_LOG_ERROR, "Failed to create sk_lock mutex.");
[180]1648    goto fail;
[182]1649  }
[180]1650
[250]1651  if(pthread_mutex_init(&rb->nk_lock, NULL) != 0)
1652  {
1653    regfi_log_add(REGFI_LOG_ERROR, "Failed to create nk_lock mutex.");
1654    goto fail;
1655  }
1656
[228]1657  if(pthread_mutex_init(&rb->mem_lock, NULL) != 0)
1658  {
1659    regfi_log_add(REGFI_LOG_ERROR, "Failed to create mem_lock mutex.");
1660    goto fail;
1661  }
1662
[99]1663  rb->hbins = range_list_new();
[110]1664  if(rb->hbins == NULL)
[182]1665  {
1666    regfi_log_add(REGFI_LOG_ERROR, "Failed to create HBIN range_list.");
[180]1667    goto fail;
[182]1668  }
[223]1669  talloc_reparent(NULL, rb, rb->hbins);
[150]1670
[106]1671  rla = true;
[135]1672  hbin_off = REGFI_REGF_SIZE;
[110]1673  hbin = regfi_parse_hbin(rb, hbin_off, true);
[106]1674  while(hbin && rla)
1675  {
[137]1676    rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin);
[148]1677    if(rla)
[223]1678      talloc_reparent(NULL, rb->hbins, hbin);
[180]1679
[106]1680    hbin_off = hbin->file_off + hbin->block_size;
[110]1681    hbin = regfi_parse_hbin(rb, hbin_off, true);
[106]1682  }
1683
[146]1684  /* This secret isn't very secret, but we don't need a good one.  This
1685   * secret is just designed to prevent someone from trying to blow our
1686   * caching and make things slow.
1687   */
1688  cache_secret = 0x15DEAD05^time(NULL)^(getpid()<<16);
1689
[250]1690  rb->sk_cache = NULL;
1691  if(REGFI_CACHE_SK_MAX > 0)
1692    rb->sk_cache = lru_cache_create_ctx(rb, REGFI_CACHE_SK_MAX, 
1693                                        cache_secret, true);
[146]1694
[250]1695  rb->nk_cache = NULL;
1696  if(REGFI_CACHE_NK_MAX > 0)
1697    rb->nk_cache = lru_cache_create_ctx(rb, REGFI_CACHE_NK_MAX, 
1698                                        cache_secret, true);
1699
[31]1700  /* success */
[186]1701  talloc_set_destructor(rb, regfi_free_cb);
[31]1702  return rb;
[180]1703
1704 fail:
[186]1705  pthread_mutex_destroy(&rb->cb_lock);
1706  pthread_rwlock_destroy(&rb->hbins_lock);
1707  pthread_mutex_destroy(&rb->sk_lock);
[250]1708  pthread_mutex_destroy(&rb->nk_lock);
[228]1709  pthread_mutex_destroy(&rb->mem_lock);
[180]1710
1711  range_list_free(rb->hbins);
1712  talloc_free(rb);
1713  return NULL;
[30]1714}
1715
1716
[148]1717/******************************************************************************
1718 ******************************************************************************/
[186]1719void regfi_free(REGFI_FILE* file)
[166]1720{
[186]1721  /* Callback handles cleanup side effects */
[150]1722  talloc_free(file);
[30]1723}
1724
1725
[80]1726/******************************************************************************
[158]1727 * First checks the offset given by the file header, then checks the
1728 * rest of the file if that fails.
[148]1729 ******************************************************************************/
[215]1730const REGFI_NK* regfi_get_rootkey(REGFI_FILE* file)
[30]1731{
[203]1732  REGFI_NK* nk = NULL;
[146]1733  REGFI_HBIN* hbin;
[168]1734  uint32_t root_offset, i, num_hbins;
[99]1735 
1736  if(!file)
[31]1737    return NULL;
[99]1738
[158]1739  root_offset = file->root_cell+REGFI_REGF_SIZE;
[206]1740  nk = regfi_load_key(file, root_offset, file->string_encoding, true);
[158]1741  if(nk != NULL)
1742  {
[161]1743    if(nk->flags & REGFI_NK_FLAG_ROOT)
[158]1744      return nk;
1745  }
1746
[182]1747  regfi_log_add(REGFI_LOG_WARN, "File header indicated root key at"
1748                " location 0x%.8X, but no root key found."
1749                " Searching rest of file...", root_offset);
[158]1750 
1751  /* If the file header gives bad info, scan through the file one HBIN
1752   * block at a time looking for an NK record with a root key type.
[146]1753   */
[180]1754 
[215]1755  if(!regfi_read_lock(file, &file->hbins_lock, "regfi_get_rootkey"))
[180]1756    return NULL;
1757
[107]1758  num_hbins = range_list_size(file->hbins);
[158]1759  for(i=0; i < num_hbins && nk == NULL; i++)
[99]1760  {
[135]1761    hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data;
[206]1762    nk = regfi_find_root_nk(file, hbin, file->string_encoding);
[31]1763  }
[30]1764
[215]1765  if(!regfi_rw_unlock(file, &file->hbins_lock, "regfi_get_rootkey"))
[180]1766    return NULL;
1767
[80]1768  return nk;
[30]1769}
1770
1771
[80]1772/******************************************************************************
1773 *****************************************************************************/
[228]1774void regfi_free_record(REGFI_FILE* file, const void* record)
[30]1775{
[228]1776  if(!regfi_lock(file, &file->mem_lock, "regfi_free_record"))
1777    return;
1778
[184]1779  talloc_unlink(NULL, (void*)record);
[228]1780
1781  regfi_unlock(file, &file->mem_lock, "regfi_free_record");
[150]1782}
[127]1783
[80]1784
[224]1785/******************************************************************************
1786 *****************************************************************************/
[252]1787const void* regfi_reference_record(REGFI_FILE* file, const void* record)
[224]1788{
[252]1789  const void* ret_val = NULL;
1790
[228]1791  if(!regfi_lock(file, &file->mem_lock, "regfi_reference_record"))
1792    return ret_val;
1793
[252]1794  ret_val = talloc_reference(NULL, record);
1795
[228]1796  regfi_unlock(file, &file->mem_lock, "regfi_reference_record");
1797  return ret_val;
[224]1798}
[80]1799
[207]1800
[80]1801/******************************************************************************
1802 *****************************************************************************/
[207]1803uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key)
1804{
1805  uint32_t num_in_list = 0;
[215]1806  if(key == NULL)
1807    return 0;
1808
[207]1809  if(key->subkeys != NULL)
1810    num_in_list = key->subkeys->num_keys;
1811
1812  if(num_in_list != key->num_subkeys)
1813  {
1814    regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d keys in its"
1815                  " subkey list but reports %d should be available.", 
1816                  key->offset, num_in_list, key->num_subkeys);
1817    return (num_in_list < key->num_subkeys)?num_in_list:key->num_subkeys;
1818  }
1819 
1820  return num_in_list;
1821}
1822
1823
1824/******************************************************************************
1825 *****************************************************************************/
1826uint32_t regfi_fetch_num_values(const REGFI_NK* key)
1827{
1828  uint32_t num_in_list = 0;
[215]1829  if(key == NULL)
1830    return 0;
1831
[207]1832  if(key->values != NULL)
1833    num_in_list = key->values->num_values;
1834
1835  if(num_in_list != key->num_values)
1836  {
1837    regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d values in"
1838                  " its value list but reports %d should be available.",
1839                  key->offset, num_in_list, key->num_values);
1840    return (num_in_list < key->num_values)?num_in_list:key->num_values;
1841  }
1842 
1843  return num_in_list;
1844}
1845
1846
1847/******************************************************************************
1848 *****************************************************************************/
[206]1849REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file)
[80]1850{
[203]1851  REGFI_NK* root;
[161]1852  REGFI_ITERATOR* ret_val;
1853
1854  ret_val = talloc(NULL, REGFI_ITERATOR);
[80]1855  if(ret_val == NULL)
1856    return NULL;
[249]1857 
1858  ret_val->cur = talloc(ret_val, REGFI_ITER_POSITION);
1859  if(ret_val->cur == NULL)
[80]1860  {
[150]1861    talloc_free(ret_val);
[80]1862    return NULL;
1863  }
1864
[135]1865  ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH);
[80]1866  if(ret_val->key_positions == NULL)
1867  {
[150]1868    talloc_free(ret_val);
[80]1869    return NULL;
1870  }
[223]1871  talloc_reparent(NULL, ret_val, ret_val->key_positions);
[80]1872
[249]1873  root = (REGFI_NK*)regfi_get_rootkey(file);
1874  if(root == NULL)
1875  {
1876    talloc_free(ret_val);
1877    return NULL;
1878  }
1879
1880  ret_val->cur->offset = root->offset;
1881  if(root->subkeys_off == REGFI_OFFSET_NONE)
1882    ret_val->cur->num_subkeys = 0;
1883  else
1884    ret_val->cur->num_subkeys = regfi_fetch_num_subkeys(root);
1885 
1886  if(root->values_off == REGFI_OFFSET_NONE)
1887    ret_val->cur->num_values = 0;
1888  else
1889    ret_val->cur->num_values = regfi_fetch_num_values(root);
1890
1891  ret_val->cur->cur_subkey = 0;
1892  ret_val->cur->cur_value = 0;
[159]1893  ret_val->f = file;
[249]1894
1895  regfi_free_record(ret_val->f, root);
[80]1896  return ret_val;
1897}
1898
1899
1900/******************************************************************************
1901 *****************************************************************************/
1902void regfi_iterator_free(REGFI_ITERATOR* i)
1903{
[228]1904  talloc_unlink(NULL, i);
[80]1905}
1906
1907
1908/******************************************************************************
1909 *****************************************************************************/
1910/* XXX: some way of indicating reason for failure should be added. */
1911bool regfi_iterator_down(REGFI_ITERATOR* i)
1912{
[203]1913  REGFI_NK* subkey;
[249]1914  REGFI_ITER_POSITION* pos = talloc(i, REGFI_ITER_POSITION);
[80]1915  if(pos == NULL)
1916    return false;
1917
[203]1918  subkey = (REGFI_NK*)regfi_iterator_cur_subkey(i);
[80]1919  if(subkey == NULL)
1920  {
[150]1921    talloc_free(pos);
[80]1922    return false;
1923  }
1924
[249]1925  if(!void_stack_push(i->key_positions, i->cur))
[80]1926  {
[150]1927    talloc_free(pos);
[249]1928    regfi_free_record(i->f, subkey);
[80]1929    return false;
[249]1930  }
[80]1931
[249]1932  pos->offset = subkey->offset;
1933  if(subkey->subkeys_off == REGFI_OFFSET_NONE)
1934    pos->num_subkeys = 0;
1935  else
1936    pos->num_subkeys = regfi_fetch_num_subkeys(subkey);
[80]1937
[249]1938  if(subkey->values_off == REGFI_OFFSET_NONE)
1939    pos->num_values = 0;
1940  else
1941    pos->num_values = regfi_fetch_num_values(subkey);
1942
1943  pos->cur_subkey = 0;
1944  pos->cur_value = 0;
1945  i->cur = pos;
1946
1947  regfi_free_record(i->f, subkey);
[80]1948  return true;
1949}
1950
1951
1952/******************************************************************************
1953 *****************************************************************************/
1954bool regfi_iterator_up(REGFI_ITERATOR* i)
1955{
1956  REGFI_ITER_POSITION* pos;
1957
1958  pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions);
1959  if(pos == NULL)
1960    return false;
1961
[228]1962  if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_iterator_up"))
1963    return false;
1964 
[249]1965  talloc_unlink(i, i->cur);
1966
[228]1967  regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_up");
1968
[249]1969  i->cur = pos;
[80]1970  return true;
1971}
1972
1973
1974/******************************************************************************
1975 *****************************************************************************/
1976bool regfi_iterator_to_root(REGFI_ITERATOR* i)
1977{
1978  while(regfi_iterator_up(i))
1979    continue;
1980
1981  return true;
1982}
1983
1984
1985/******************************************************************************
1986 *****************************************************************************/
[207]1987bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name)
[80]1988{
[249]1989  const REGFI_NK* cur_key;
[207]1990  uint32_t new_index;
[249]1991  bool ret_val = false;
[133]1992
[249]1993  cur_key = regfi_iterator_cur_key(i);
1994  if(cur_key == NULL)
[260]1995  {
[257]1996    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in find_subkey.");
[249]1997    return ret_val;
[260]1998  }
[249]1999
2000  if(regfi_find_subkey(i->f, cur_key, name, &new_index))
[80]2001  {
[249]2002    i->cur->cur_subkey = new_index;
2003    ret_val = true;
[80]2004  }
2005
[249]2006  regfi_free_record(i->f, cur_key);
2007  return ret_val;
[80]2008}
2009
2010
2011/******************************************************************************
2012 *****************************************************************************/
[252]2013bool regfi_iterator_descend(REGFI_ITERATOR* i, const char** path)
[80]2014{
[168]2015  uint32_t x;
[80]2016  if(path == NULL)
2017    return false;
2018
2019  for(x=0; 
2020      ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x])
2021       && regfi_iterator_down(i));
2022      x++)
2023  { continue; }
2024
2025  if(path[x] == NULL)
[215]2026  {
[80]2027    return true;
[215]2028  }
2029
[80]2030  /* XXX: is this the right number of times? */
2031  for(; x > 0; x--)
2032    regfi_iterator_up(i);
2033 
2034  return false;
2035}
2036
2037
2038/******************************************************************************
2039 *****************************************************************************/
[203]2040const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i)
[80]2041{
[228]2042  const REGFI_NK* ret_val = NULL;
2043
[249]2044  ret_val = regfi_load_key(i->f, i->cur->offset, i->f->string_encoding, true);
[228]2045  return ret_val;
[80]2046}
2047
2048
2049/******************************************************************************
2050 *****************************************************************************/
[206]2051const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key)
[109]2052{
[206]2053  if(key == NULL || key->sk_off == REGFI_OFFSET_NONE)
[109]2054    return NULL;
2055
[206]2056  return regfi_load_sk(file, key->sk_off + REGFI_REGF_SIZE, true);
[109]2057}
2058
2059
2060/******************************************************************************
2061 *****************************************************************************/
[253]2062const REGFI_SK* regfi_next_sk(REGFI_FILE* file, const REGFI_SK* sk)
2063{
2064  if(sk == NULL || sk->next_sk_off == REGFI_OFFSET_NONE)
2065    return NULL;
2066
2067  return regfi_load_sk(file, sk->next_sk_off + REGFI_REGF_SIZE, true);
2068}
2069
2070
2071/******************************************************************************
2072 *****************************************************************************/
2073const REGFI_SK* regfi_prev_sk(REGFI_FILE* file, const REGFI_SK* sk)
2074{
2075  if(sk == NULL || sk->prev_sk_off == REGFI_OFFSET_NONE)
2076    return NULL;
2077
2078  return regfi_load_sk(file, sk->prev_sk_off + REGFI_REGF_SIZE, true);
2079}
2080
2081
2082/******************************************************************************
2083 *****************************************************************************/
[199]2084bool regfi_iterator_first_subkey(REGFI_ITERATOR* i)
[80]2085{
[249]2086  i->cur->cur_subkey = 0;
2087  return (i->cur->cur_subkey < i->cur->num_subkeys);
[80]2088}
2089
2090
2091/******************************************************************************
2092 *****************************************************************************/
[203]2093const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i)
[80]2094{
[249]2095  const REGFI_NK* cur_key;
2096  const REGFI_NK* ret_val;
2097 
2098  cur_key = regfi_iterator_cur_key(i);
2099  if(cur_key == NULL)
[260]2100  {
[257]2101    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in cur_subkey.");
[249]2102    return NULL;
[260]2103  }
[249]2104
2105  ret_val = regfi_get_subkey(i->f, cur_key, i->cur->cur_subkey);
2106
2107  regfi_free_record(i->f, cur_key);
2108  return ret_val;
[30]2109}
[80]2110
2111
2112/******************************************************************************
2113 *****************************************************************************/
[199]2114bool regfi_iterator_next_subkey(REGFI_ITERATOR* i)
[80]2115{
[249]2116  i->cur->cur_subkey++;
2117  return (i->cur->cur_subkey < i->cur->num_subkeys);
[80]2118}
2119
2120
2121/******************************************************************************
2122 *****************************************************************************/
[207]2123bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name)
[80]2124{
[249]2125  const REGFI_NK* cur_key;
[207]2126  uint32_t new_index;
[249]2127  bool ret_val = false;
[80]2128
[249]2129  cur_key = regfi_iterator_cur_key(i);
2130  if(cur_key == NULL)
[260]2131  {
[257]2132    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in find_value.");
[249]2133    return ret_val;
[260]2134  }
[249]2135
2136  if(regfi_find_value(i->f, cur_key, name, &new_index))
[80]2137  {
[249]2138    i->cur->cur_value = new_index;
2139    ret_val = true;
[80]2140  }
2141
[249]2142  regfi_free_record(i->f, cur_key);
2143  return ret_val;
[80]2144}
2145
2146
2147/******************************************************************************
2148 *****************************************************************************/
[199]2149bool regfi_iterator_first_value(REGFI_ITERATOR* i)
[80]2150{
[249]2151  i->cur->cur_value = 0;
2152  return (i->cur->cur_value < i->cur->num_values);
[80]2153}
2154
2155
2156/******************************************************************************
2157 *****************************************************************************/
[203]2158const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i)
[80]2159{
[249]2160  const REGFI_NK* cur_key;
2161  const REGFI_VK* ret_val = NULL;
2162
2163  cur_key = regfi_iterator_cur_key(i);
2164  if(cur_key == NULL)
[260]2165  {
[257]2166    regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in cur_value.");
[249]2167    return ret_val;
[260]2168  }
[249]2169
2170  ret_val = regfi_get_value(i->f, cur_key, i->cur->cur_value);
2171 
2172  regfi_free_record(i->f, cur_key);
2173  return ret_val;
[80]2174}
2175
2176
2177/******************************************************************************
2178 *****************************************************************************/
[199]2179bool regfi_iterator_next_value(REGFI_ITERATOR* i)
[80]2180{
[249]2181  i->cur->cur_value++;
2182  return (i->cur->cur_value < i->cur->num_values);
[80]2183}
[97]2184
2185
[249]2186
2187
[159]2188/******************************************************************************
2189 *****************************************************************************/
[252]2190const REGFI_NK** regfi_iterator_ancestry(REGFI_ITERATOR* i)
[249]2191{
2192  REGFI_NK** ret_val;
2193  void_stack_iterator* iter;
2194  const REGFI_ITER_POSITION* cur;
[250]2195  uint16_t k, num_keys;
[249]2196
[250]2197  num_keys = void_stack_size(i->key_positions)+1;
2198  ret_val = talloc_array(NULL, REGFI_NK*, num_keys+1);
[249]2199  if(ret_val == NULL)
2200    return NULL;
2201
2202  iter = void_stack_iterator_new(i->key_positions);
2203  if (iter == NULL)
2204  {
2205    talloc_free(ret_val);
2206    return NULL;
2207  }
[250]2208
2209  k=0;
2210  for(cur=void_stack_iterator_next(iter);
2211      cur != NULL; cur=void_stack_iterator_next(iter))
2212  { 
2213    ret_val[k++] = regfi_load_key(i->f, cur->offset, i->f->string_encoding, true); 
2214  }
2215  ret_val[k] = regfi_load_key(i->f, i->cur->offset, i->f->string_encoding, true);
2216  void_stack_iterator_free(iter);
2217
[252]2218  if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_iterator_ancestry"))
[249]2219  {
2220    talloc_free(ret_val);
2221    return NULL;
2222  }
2223
[250]2224  for(k=0; k<num_keys; k++)
[249]2225    talloc_reparent(NULL, ret_val, ret_val[k]);
2226
[252]2227  regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_ancestry");
[249]2228
[250]2229  ret_val[k] = NULL;
[249]2230  return (const REGFI_NK**)ret_val;
2231}
2232
2233
2234/******************************************************************************
2235 *****************************************************************************/
[206]2236const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file,
2237                                             const REGFI_NK* key)
[160]2238{
2239  REGFI_CLASSNAME* ret_val;
[168]2240  uint8_t* raw;
[262]2241  REGFI_BUFFER tmp_buf;
[168]2242  uint32_t offset;
[262]2243  int32_t max_size;
[168]2244  uint16_t parse_length;
[160]2245
2246  if(key->classname_off == REGFI_OFFSET_NONE || key->classname_length == 0)
2247    return NULL;
2248
2249  offset = key->classname_off + REGFI_REGF_SIZE;
[206]2250  max_size = regfi_calc_maxsize(file, offset);
[160]2251  if(max_size <= 0)
2252    return NULL;
2253
2254  parse_length = key->classname_length;
[206]2255  raw = regfi_parse_classname(file, offset, &parse_length, max_size, true);
[160]2256 
2257  if(raw == NULL)
2258  {
[182]2259    regfi_log_add(REGFI_LOG_WARN, "Could not parse class"
2260                  " name at offset 0x%.8X for key record at offset 0x%.8X.",
2261                  offset, key->offset);
[160]2262    return NULL;
2263  }
2264
2265  ret_val = talloc(NULL, REGFI_CLASSNAME);
2266  if(ret_val == NULL)
2267    return NULL;
2268
[206]2269  ret_val->offset = offset;
[160]2270  ret_val->raw = raw;
2271  ret_val->size = parse_length;
[223]2272  talloc_reparent(NULL, ret_val, raw);
[160]2273
[262]2274  tmp_buf = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
2275                               regfi_encoding_int2str(file->string_encoding),
2276                               raw, parse_length);
2277  if(tmp_buf.buf == NULL)
[160]2278  {
[182]2279    regfi_log_add(REGFI_LOG_WARN, "Error occurred while"
2280                  " converting classname to charset %s.  Error message: %s",
[262]2281                  file->string_encoding, strerror(errno));
[160]2282    ret_val->interpreted = NULL;
2283  }
2284  else
2285  {
[262]2286    ret_val->interpreted = (char*)tmp_buf.buf;
2287    talloc_reparent(NULL, ret_val, tmp_buf.buf);
[160]2288  }
2289
2290  return ret_val;
2291}
2292
2293
2294/******************************************************************************
2295 *****************************************************************************/
[206]2296const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file, 
2297                                   const REGFI_VK* value)
[159]2298{
2299  REGFI_DATA* ret_val = NULL;
2300  REGFI_BUFFER raw_data;
2301
2302  if(value->data_size != 0)
2303  {
[206]2304    raw_data = regfi_load_data(file, value->data_off, value->data_size,
[209]2305                               value->data_in_offset, true);
[159]2306    if(raw_data.buf == NULL)
2307    {
[182]2308      regfi_log_add(REGFI_LOG_WARN, "Could not parse data record"
2309                    " while parsing VK record at offset 0x%.8X.",
2310                    value->offset);
[159]2311    }
2312    else
2313    {
2314      ret_val = regfi_buffer_to_data(raw_data);
2315
2316      if(ret_val == NULL)
2317      {
[182]2318        regfi_log_add(REGFI_LOG_WARN, "Error occurred in converting"
2319                      " data buffer to data structure while interpreting "
2320                      "data for VK record at offset 0x%.8X.",
2321                      value->offset);
[159]2322        talloc_free(raw_data.buf);
2323        return NULL;
2324      }
2325
[206]2326      if(!regfi_interpret_data(file, file->string_encoding, 
2327                               value->type, ret_val))
[159]2328      {
[182]2329        regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
2330                      " interpreting data for VK record at offset 0x%.8X.",
2331                      value->offset);
[159]2332      }
2333    }
2334  }
2335 
2336  return ret_val;
2337}
2338
2339
[207]2340
[159]2341/******************************************************************************
2342 *****************************************************************************/
[207]2343bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key, 
2344                       const char* name, uint32_t* index)
2345{
2346  const REGFI_NK* cur;
2347  uint32_t i;
2348  uint32_t num_subkeys = regfi_fetch_num_subkeys(key);
2349  bool found = false;
2350
[256]2351  /* XXX: should we allow "(default)" subkey names?
2352   *      Do realistically they exist?
[207]2353   */
2354  if(name == NULL)
2355    return false;
2356
[262]2357  /* XXX: Should lazily build a hash table in memory to index where keys are when
2358   *      there are a large number of subkeys.  Attach this to cached keys to
2359   *      bound the extra amount of memory used.
2360   */
[207]2361  for(i=0; (i < num_subkeys) && (found == false); i++)
2362  {
2363    cur = regfi_get_subkey(file, key, i);
2364    if(cur == NULL)
2365      return false;
2366
[256]2367    /* A NULL name signifies the "(default)" value for a key */
2368    if(cur->name != NULL && (strcasecmp(cur->name, name) == 0))
[207]2369    {
2370      found = true;
2371      *index = i;
2372    }
2373
[228]2374    regfi_free_record(file, cur);
[207]2375  }
2376
2377  return found;
2378}
2379
2380
2381
2382/******************************************************************************
2383 *****************************************************************************/
2384bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key, 
2385                      const char* name, uint32_t* index)
2386{
2387  const REGFI_VK* cur;
2388  uint32_t i;
2389  uint32_t num_values = regfi_fetch_num_values(key);
2390  bool found = false;
2391
[262]2392  /* XXX: Should lazily build a hash table in memory to index where values are when
2393   *      there are a large number of them.  Attach this to cached keys to
2394   *      bound the extra amount of memory used.
2395   */
[207]2396  for(i=0; (i < num_values) && (found == false); i++)
2397  {
2398    cur = regfi_get_value(file, key, i);
2399    if(cur == NULL)
2400      return false;
2401
[256]2402    /* A NULL name signifies the "(default)" value for a key */
2403    if(((name == NULL) && (cur->name == NULL))
2404       || ((name != NULL) && (cur->name != NULL) 
2405           && (strcasecmp(cur->name, name) == 0)))
[207]2406    {
2407      found = true;
2408      *index = i;
2409    }
2410
[228]2411    regfi_free_record(file, cur);
[207]2412  }
2413
2414  return found;
2415}
2416
2417
2418
2419/******************************************************************************
2420 *****************************************************************************/
2421const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key, 
2422                                 uint32_t index)
2423{
2424  if(index < regfi_fetch_num_subkeys(key))
2425  {
2426    return regfi_load_key(file, 
2427                          key->subkeys->elements[index].offset+REGFI_REGF_SIZE,
2428                          file->string_encoding, true);
2429  }
2430
2431  return NULL;
2432}
2433
2434
2435/******************************************************************************
2436 *****************************************************************************/
2437const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, 
2438                                uint32_t index)
2439{
2440  if(index < regfi_fetch_num_values(key))
2441  {
2442    return regfi_load_value(file, 
2443                            key->values->elements[index]+REGFI_REGF_SIZE,
2444                            file->string_encoding, true);
2445  }
2446
2447  return NULL; 
2448}
2449
2450
[215]2451
[207]2452/******************************************************************************
2453 *****************************************************************************/
[215]2454const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key)
2455{
2456  if(key != NULL && key->parent_off != REGFI_OFFSET_NONE)
2457    return regfi_load_key(file, 
2458                          key->parent_off+REGFI_REGF_SIZE,
2459                          file->string_encoding, true);
[228]2460
[215]2461  return NULL;
2462}
2463
2464
2465
2466/******************************************************************************
2467 *****************************************************************************/
[159]2468REGFI_DATA* regfi_buffer_to_data(REGFI_BUFFER raw_data)
2469{
2470  REGFI_DATA* ret_val;
2471
2472  if(raw_data.buf == NULL)
2473    return NULL;
2474
2475  ret_val = talloc(NULL, REGFI_DATA);
2476  if(ret_val == NULL)
2477    return NULL;
2478 
[223]2479  talloc_reparent(NULL, ret_val, raw_data.buf);
[159]2480  ret_val->raw = raw_data.buf;
2481  ret_val->size = raw_data.len;
2482  ret_val->interpreted_size = 0;
2483  ret_val->interpreted.qword = 0;
2484
2485  return ret_val;
2486}
2487
2488
2489/******************************************************************************
2490 *****************************************************************************/
[161]2491bool regfi_interpret_data(REGFI_FILE* file, REGFI_ENCODING string_encoding,
[168]2492                          uint32_t type, REGFI_DATA* data)
[159]2493{
[262]2494  REGFI_BUFFER tmp_buf;
[168]2495  uint8_t** tmp_array;
[262]2496  uint32_t i, j;
[159]2497
2498  if(data == NULL)
2499    return false;
2500
2501  switch (type)
2502  {
2503  case REG_SZ:
2504  case REG_EXPAND_SZ:
2505  /* REG_LINK is a symbolic link, stored as a unicode string. */
2506  case REG_LINK:
[262]2507    tmp_buf = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
2508                                 regfi_encoding_int2str(string_encoding),
2509                                 data->raw, data->size);
2510    if(tmp_buf.buf == NULL)
[159]2511    {
[182]2512      regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
[262]2513                    " converting data of type %d to string encoding %d."
2514                    "  Error message: %s",
2515                    type, string_encoding, strerror(errno));
[159]2516      data->interpreted.string = NULL;
2517      data->interpreted_size = 0;
2518      return false;
2519    }
2520
[262]2521    data->interpreted.string = tmp_buf.buf;
2522    data->interpreted_size = tmp_buf.len;
2523    talloc_reparent(NULL, data, tmp_buf.buf);
[159]2524    break;
2525
2526  case REG_DWORD:
2527    if(data->size < 4)
2528    {
2529      data->interpreted.dword = 0;
2530      data->interpreted_size = 0;
2531      return false;
2532    }
2533    data->interpreted.dword = IVAL(data->raw, 0);
2534    data->interpreted_size = 4;
2535    break;
2536
2537  case REG_DWORD_BE:
2538    if(data->size < 4)
2539    {
2540      data->interpreted.dword_be = 0;
2541      data->interpreted_size = 0;
2542      return false;
2543    }
2544    data->interpreted.dword_be = RIVAL(data->raw, 0);
2545    data->interpreted_size = 4;
2546    break;
2547
2548  case REG_QWORD:
2549    if(data->size < 8)
2550    {
2551      data->interpreted.qword = 0;
2552      data->interpreted_size = 0;
2553      return false;
2554    }
2555    data->interpreted.qword = 
[168]2556      (uint64_t)IVAL(data->raw, 0) + (((uint64_t)IVAL(data->raw, 4))<<32);
[159]2557    data->interpreted_size = 8;
2558    break;
2559   
2560  case REG_MULTI_SZ:
2561    /* Attempt to convert entire string from UTF-16LE to output encoding,
2562     * then parse and quote fields individually.
2563     */
[262]2564    tmp_buf = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE),
2565                                 regfi_encoding_int2str(string_encoding),
2566                                 data->raw, data->size);
2567    if(tmp_buf.buf == NULL)
[159]2568    {
[182]2569      regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
[262]2570                    " converting data of type %d to string encoding %d."
2571                    "  Error message: %s",
2572                    type, string_encoding, strerror(errno));
[159]2573      data->interpreted.multiple_string = NULL;
2574      data->interpreted_size = 0;
2575      return false;
2576    }
2577
[262]2578    tmp_array = talloc_array(NULL, uint8_t*, tmp_buf.len+1);
[159]2579    if(tmp_array == NULL)
2580    {
[262]2581      talloc_free(tmp_buf.buf);
[159]2582      data->interpreted.string = NULL;
2583      data->interpreted_size = 0;
2584      return false;
2585    }
[262]2586
2587    tmp_array[0] = tmp_buf.buf;
2588    for(i=0,j=1; i < tmp_buf.len && j < tmp_buf.len; i++)
[159]2589    {
[262]2590      if(tmp_buf.buf[i] == '\0' && (i+1 < tmp_buf.len) 
2591         && tmp_buf.buf[i+1] != '\0')
2592        tmp_array[j++] = tmp_buf.buf+i+1;
[159]2593    }
2594    tmp_array[j] = NULL;
[168]2595    tmp_array = talloc_realloc(NULL, tmp_array, uint8_t*, j+1);
[159]2596    data->interpreted.multiple_string = tmp_array;
2597    /* XXX: how meaningful is this?  should we store number of strings instead? */
[262]2598    data->interpreted_size = tmp_buf.len;
2599    talloc_reparent(NULL, tmp_array, tmp_buf.buf);
[223]2600    talloc_reparent(NULL, data, tmp_array);
[159]2601    break;
2602
2603  /* XXX: Dont know how to interpret these yet, just treat as binary */
2604  case REG_NONE:
2605    data->interpreted.none = data->raw;
2606    data->interpreted_size = data->size;
2607    break;
2608
2609  case REG_RESOURCE_LIST:
2610    data->interpreted.resource_list = data->raw;
2611    data->interpreted_size = data->size;
2612    break;
2613
2614  case REG_FULL_RESOURCE_DESCRIPTOR:
2615    data->interpreted.full_resource_descriptor = data->raw;
2616    data->interpreted_size = data->size;
2617    break;
2618
2619  case REG_RESOURCE_REQUIREMENTS_LIST:
2620    data->interpreted.resource_requirements_list = data->raw;
2621    data->interpreted_size = data->size;
2622    break;
2623
2624  case REG_BINARY:
2625    data->interpreted.binary = data->raw;
2626    data->interpreted_size = data->size;
2627    break;
2628
2629  default:
2630    data->interpreted.qword = 0;
2631    data->interpreted_size = 0;
2632    return false;
2633  }
2634
2635  data->type = type;
2636  return true;
2637}
2638
2639
[166]2640/******************************************************************************
[262]2641 * Convert string from input_charset to output_charset.
2642 * On error, returns a NULL buf attribute and sets the errno.
[166]2643 *****************************************************************************/
[262]2644REGFI_BUFFER regfi_conv_charset(const char* input_charset, const char* output_charset,
2645                                uint8_t* input, uint32_t input_len)
[159]2646{
2647  iconv_t conv_desc;
2648  char* inbuf = (char*)input;
[262]2649  char* outbuf;
2650  char* retbuf;
2651  size_t allocated = (size_t)input_len;
2652  size_t in_left = (size_t)input_len;
2653  size_t out_left = (size_t)allocated-1;
2654  REGFI_BUFFER ret_val;
[159]2655  int ret;
2656
[262]2657  ret_val.buf = NULL;
2658  ret_val.len = 0;
2659  retbuf = talloc_array(NULL, char, allocated);
2660  outbuf = retbuf;
2661  if(outbuf == NULL)
2662  {
2663    errno = ENOMEM;
2664    return ret_val;
2665  }
2666
2667  /* Set up conversion descriptor. */
[161]2668  /* XXX: Consider creating a couple of conversion descriptors earlier,
2669   *      storing them on an iterator so they don't have to be recreated
2670   *      each time.
2671   */
2672  conv_desc = iconv_open(output_charset, input_charset);
[159]2673
[262]2674  ret = 0;
2675  do
2676  {
2677    if(ret == -1)
2678    {
2679      retbuf = talloc_realloc(NULL, retbuf, char, allocated+(in_left*2));
2680      if(retbuf == NULL)
2681      {
2682        errno = ENOMEM;
2683        return ret_val;
2684      }
2685      outbuf = retbuf+(allocated-1-out_left);
2686      out_left += in_left*2;
2687      allocated += in_left*2;
2688    }
2689    ret = iconv(conv_desc, &inbuf, &in_left, &outbuf, &out_left);
2690   
2691  } while(ret == -1 && errno == E2BIG);
2692 
[159]2693  if(ret == -1)
2694  {
2695    iconv_close(conv_desc);
[262]2696    return ret_val;
[159]2697  }
2698
[262]2699  /* Save memory */
2700  if(out_left > 0)
2701  {
2702    retbuf = talloc_realloc(NULL, retbuf, char, allocated-out_left);
2703    if(retbuf == NULL)
2704    {
2705      errno = ENOMEM;
2706      return ret_val;
2707    }
2708    allocated -= out_left;
2709  }
2710  retbuf[allocated-1] = '\0';
2711  iconv_close(conv_desc);
2712
2713  ret_val.buf = (uint8_t*)retbuf;
2714  ret_val.len = allocated-1;
2715  return ret_val;
[159]2716}
2717
2718
2719/*******************************************************************
[97]2720 * Computes the checksum of the registry file header.
[159]2721 * buffer must be at least the size of a regf header (4096 bytes).
[97]2722 *******************************************************************/
[168]2723static uint32_t regfi_compute_header_checksum(uint8_t* buffer)
[97]2724{
[168]2725  uint32_t checksum, x;
[97]2726  int i;
2727
2728  /* XOR of all bytes 0x0000 - 0x01FB */
2729
2730  checksum = x = 0;
2731 
2732  for ( i=0; i<0x01FB; i+=4 ) {
2733    x = IVAL(buffer, i );
2734    checksum ^= x;
2735  }
2736 
2737  return checksum;
2738}
2739
2740
2741/*******************************************************************
2742 *******************************************************************/
[178]2743REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict)
[97]2744{
[168]2745  uint8_t file_header[REGFI_REGF_SIZE];
2746  uint32_t length;
[135]2747  REGFI_FILE* ret_val;
[97]2748
[150]2749  ret_val = talloc(NULL, REGFI_FILE);
[97]2750  if(ret_val == NULL)
2751    return NULL;
2752
[150]2753  ret_val->sk_cache = NULL;
2754  ret_val->hbins = NULL;
[178]2755
[135]2756  length = REGFI_REGF_SIZE;
[178]2757  if((regfi_read(file_cb, file_header, &length)) != 0 
2758     || length != REGFI_REGF_SIZE)
[182]2759  {
2760    regfi_log_add(REGFI_LOG_WARN, "Read failed while parsing REGF structure.");
[150]2761    goto fail;
[182]2762  }
2763
[97]2764  ret_val->checksum = IVAL(file_header, 0x1FC);
2765  ret_val->computed_checksum = regfi_compute_header_checksum(file_header);
2766  if (strict && (ret_val->checksum != ret_val->computed_checksum))
[182]2767  {
2768    regfi_log_add(REGFI_LOG_WARN, "Stored header checksum (%.8X) did not equal"
2769                  " computed checksum (%.8X).",
2770                  ret_val->checksum, ret_val->computed_checksum);
2771    if(strict)
2772      goto fail;
2773  }
[97]2774
[135]2775  memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE);
[150]2776  if(memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0)
[97]2777  {
[182]2778    regfi_log_add(REGFI_LOG_ERROR, "Magic number mismatch "
2779                  "(%.2X %.2X %.2X %.2X) while parsing hive header",
2780                  ret_val->magic[0], ret_val->magic[1], 
2781                  ret_val->magic[2], ret_val->magic[3]);
2782    goto fail;
[97]2783  }
[178]2784
[151]2785  ret_val->sequence1 = IVAL(file_header, 0x4);
2786  ret_val->sequence2 = IVAL(file_header, 0x8);
[251]2787  ret_val->mtime = ((uint64_t)IVAL(file_header, 0x10)) << 32;
2788  ret_val->mtime |= IVAL(file_header, 0xC);
[151]2789  ret_val->major_version = IVAL(file_header, 0x14);
2790  ret_val->minor_version = IVAL(file_header, 0x18);
2791  ret_val->type = IVAL(file_header, 0x1C);
2792  ret_val->format = IVAL(file_header, 0x20);
2793  ret_val->root_cell = IVAL(file_header, 0x24);
[97]2794  ret_val->last_block = IVAL(file_header, 0x28);
[151]2795  ret_val->cluster = IVAL(file_header, 0x2C);
[97]2796
[151]2797  memcpy(ret_val->file_name, file_header+0x30,  REGFI_REGF_NAME_SIZE);
2798
2799  ret_val->rm_id = winsec_parse_uuid(ret_val, file_header+0x70, 16);
[257]2800  if(ret_val->rm_id == NULL)
2801    regfi_log_add(REGFI_LOG_WARN, "Hive header's rm_id failed to parse.");
2802
[151]2803  ret_val->log_id = winsec_parse_uuid(ret_val, file_header+0x80, 16);
[257]2804  if(ret_val->log_id == NULL)
2805    regfi_log_add(REGFI_LOG_WARN, "Hive header's log_id failed to parse.");
2806
[151]2807  ret_val->flags = IVAL(file_header, 0x90);
[257]2808
[151]2809  ret_val->tm_id = winsec_parse_uuid(ret_val, file_header+0x94, 16);
[257]2810  if(ret_val->tm_id == NULL)
2811    regfi_log_add(REGFI_LOG_WARN, "Hive header's tm_id failed to parse.");
2812
[151]2813  ret_val->guid_signature = IVAL(file_header, 0xa4);
2814
2815  memcpy(ret_val->reserved1, file_header+0xa8, REGFI_REGF_RESERVED1_SIZE);
2816  memcpy(ret_val->reserved2, file_header+0x200, REGFI_REGF_RESERVED2_SIZE);
2817
2818  ret_val->thaw_tm_id = winsec_parse_uuid(ret_val, file_header+0xFC8, 16);
2819  ret_val->thaw_rm_id = winsec_parse_uuid(ret_val, file_header+0xFD8, 16);
2820  ret_val->thaw_log_id = winsec_parse_uuid(ret_val, file_header+0xFE8, 16);
[152]2821  ret_val->boot_type = IVAL(file_header, 0xFF8);
2822  ret_val->boot_recover = IVAL(file_header, 0xFFC);
[151]2823
[97]2824  return ret_val;
[150]2825
2826 fail:
2827  talloc_free(ret_val);
2828  return NULL;
[97]2829}
2830
2831
2832
[148]2833/******************************************************************************
[97]2834 * Given real file offset, read and parse the hbin at that location
[110]2835 * along with it's associated cells.
[148]2836 ******************************************************************************/
[168]2837REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, bool strict)
[97]2838{
[181]2839  REGFI_HBIN* hbin = NULL;
[168]2840  uint8_t hbin_header[REGFI_HBIN_HEADER_SIZE];
2841  uint32_t length;
[99]2842 
2843  if(offset >= file->file_length)
[180]2844    goto fail;
2845 
[186]2846  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_hbin"))
[180]2847    goto fail;
[97]2848
[178]2849  if(regfi_seek(file->cb, offset, SEEK_SET) == -1)
[137]2850  {
[182]2851    regfi_log_add(REGFI_LOG_ERROR, "Seek failed"
2852                  " while parsing hbin at offset 0x%.8X.", offset);
[180]2853    goto fail_locked;
[137]2854  }
[97]2855
[135]2856  length = REGFI_HBIN_HEADER_SIZE;
[178]2857  if((regfi_read(file->cb, hbin_header, &length) != 0) 
[135]2858     || length != REGFI_HBIN_HEADER_SIZE)
[182]2859  {
2860    regfi_log_add(REGFI_LOG_ERROR, "Read failed"
2861                  " while parsing hbin at offset 0x%.8X.", offset);
[180]2862    goto fail_locked;
[182]2863  }
[97]2864
[186]2865  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_hbin"))
[180]2866    goto fail;
[97]2867
[148]2868  hbin = talloc(NULL, REGFI_HBIN);
2869  if(hbin == NULL)
[180]2870    goto fail;
[99]2871  hbin->file_off = offset;
2872
[97]2873  memcpy(hbin->magic, hbin_header, 4);
2874  if(strict && (memcmp(hbin->magic, "hbin", 4) != 0))
[99]2875  {
[182]2876    /* This always seems to happen at the end of a file, so we make it an INFO
2877     * message, rather than something more serious.
2878     */
2879    regfi_log_add(REGFI_LOG_INFO, "Magic number mismatch "
2880                  "(%.2X %.2X %.2X %.2X) while parsing hbin at offset"
2881                  " 0x%.8X.", hbin->magic[0], hbin->magic[1], 
2882                  hbin->magic[2], hbin->magic[3], offset);
[180]2883    goto fail;
[99]2884  }
[97]2885
2886  hbin->first_hbin_off = IVAL(hbin_header, 0x4);
2887  hbin->block_size = IVAL(hbin_header, 0x8);
[182]2888  /* this should be the same thing as hbin->block_size, but just in case */
[97]2889  hbin->next_block = IVAL(hbin_header, 0x1C);
2890
2891
2892  /* Ensure the block size is a multiple of 0x1000 and doesn't run off
2893   * the end of the file.
2894   */
[116]2895  /* XXX: This may need to be relaxed for dealing with
2896   *      partial or corrupt files.
2897   */
[97]2898  if((offset + hbin->block_size > file->file_length)
2899     || (hbin->block_size & 0xFFFFF000) != hbin->block_size)
[99]2900  {
[182]2901    regfi_log_add(REGFI_LOG_ERROR, "The hbin offset is not aligned"
2902                  " or runs off the end of the file"
2903                  " while parsing hbin at offset 0x%.8X.", offset);
[180]2904    goto fail;
[99]2905  }
[97]2906
2907  return hbin;
[180]2908
2909 fail_locked:
[186]2910  regfi_unlock(file, &file->cb_lock, "regfi_parse_hbin");
[180]2911 fail:
2912  talloc_free(hbin);
2913  return NULL;
[97]2914}
2915
2916
[126]2917/*******************************************************************
2918 *******************************************************************/
[203]2919REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset, 
2920                         uint32_t max_size, bool strict)
[99]2921{
[168]2922  uint8_t nk_header[REGFI_NK_MIN_LENGTH];
[203]2923  REGFI_NK* ret_val;
[168]2924  uint32_t length,cell_length;
[101]2925  bool unalloc = false;
[99]2926
[203]2927  ret_val = talloc(NULL, REGFI_NK);
[180]2928  if(ret_val == NULL)
2929  {
[182]2930    regfi_log_add(REGFI_LOG_ERROR, "Failed to allocate memory while"
2931                  " parsing NK record at offset 0x%.8X.", offset);
[180]2932    goto fail;
2933  }
2934
[186]2935  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_nk"))
[180]2936    goto fail;
2937
[178]2938  if(!regfi_parse_cell(file->cb, offset, nk_header, REGFI_NK_MIN_LENGTH,
[101]2939                       &cell_length, &unalloc))
[137]2940  {
[182]2941    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
2942                  " while parsing NK record at offset 0x%.8X.", offset);
[180]2943    goto fail_locked;
[137]2944  }
2945
[101]2946  if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k'))
[135]2947  {
[182]2948    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing"
2949                  " NK record at offset 0x%.8X.", offset);
[180]2950    goto fail_locked;
[135]2951  }
[99]2952
[150]2953  ret_val->values = NULL;
2954  ret_val->subkeys = NULL;
[99]2955  ret_val->offset = offset;
[101]2956  ret_val->cell_size = cell_length;
2957
[99]2958  if(ret_val->cell_size > max_size)
2959    ret_val->cell_size = max_size & 0xFFFFFFF8;
2960  if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) 
[157]2961     || (strict && (ret_val->cell_size & 0x00000007) != 0))
[99]2962  {
[182]2963    regfi_log_add(REGFI_LOG_WARN, "A length check failed while"
2964                  " parsing NK record at offset 0x%.8X.", offset);
[180]2965    goto fail_locked;
[99]2966  }
2967
[101]2968  ret_val->magic[0] = nk_header[0x0];
2969  ret_val->magic[1] = nk_header[0x1];
[161]2970  ret_val->flags = SVAL(nk_header, 0x2);
[152]2971 
[161]2972  if((ret_val->flags & ~REGFI_NK_KNOWN_FLAGS) != 0)
[99]2973  {
[182]2974    regfi_log_add(REGFI_LOG_WARN, "Unknown key flags (0x%.4X) while"
2975                  " parsing NK record at offset 0x%.8X.", 
2976                  (ret_val->flags & ~REGFI_NK_KNOWN_FLAGS), offset);
[99]2977  }
[101]2978
[251]2979  ret_val->mtime = ((uint64_t)IVAL(nk_header, 0x8)) << 32;
2980  ret_val->mtime |= IVAL(nk_header, 0x4);
[116]2981  /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990
2982   * or later than Jan 1, 2290, we consider this a bad key.  This helps
2983   * weed out some false positives during deleted data recovery.
2984   */
2985  if(unalloc
[251]2986     && (ret_val->mtime < REGFI_MTIME_MIN
2987         || ret_val->mtime > REGFI_MTIME_MAX))
[180]2988  { goto fail_locked; }
[116]2989
[101]2990  ret_val->unknown1 = IVAL(nk_header, 0xC);
2991  ret_val->parent_off = IVAL(nk_header, 0x10);
2992  ret_val->num_subkeys = IVAL(nk_header, 0x14);
2993  ret_val->unknown2 = IVAL(nk_header, 0x18);
2994  ret_val->subkeys_off = IVAL(nk_header, 0x1C);
2995  ret_val->unknown3 = IVAL(nk_header, 0x20);
2996  ret_val->num_values = IVAL(nk_header, 0x24);
2997  ret_val->values_off = IVAL(nk_header, 0x28);
2998  ret_val->sk_off = IVAL(nk_header, 0x2C);
2999  ret_val->classname_off = IVAL(nk_header, 0x30);
[99]3000
[101]3001  ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34);
3002  ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38);
3003  ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C);
3004  ret_val->max_bytes_value = IVAL(nk_header, 0x40);
3005  ret_val->unk_index = IVAL(nk_header, 0x44);
[99]3006
[101]3007  ret_val->name_length = SVAL(nk_header, 0x48);
3008  ret_val->classname_length = SVAL(nk_header, 0x4A);
[206]3009  ret_val->name = NULL;
[99]3010
3011  if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size)
[101]3012  {
3013    if(strict)
3014    {
[182]3015      regfi_log_add(REGFI_LOG_ERROR, "Contents too large for cell"
3016                    " while parsing NK record at offset 0x%.8X.", offset);
[180]3017      goto fail_locked;
[101]3018    }
3019    else
3020      ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH;
3021  }
3022  else if (unalloc)
3023  { /* Truncate cell_size if it's much larger than the apparent total record length. */
3024    /* Round up to the next multiple of 8 */
3025    length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8;
3026    if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH)
3027      length+=8;
[99]3028
[101]3029    /* If cell_size is still greater, truncate. */
3030    if(length < ret_val->cell_size)
3031      ret_val->cell_size = length;
3032  }
3033
[206]3034  /* +1 to length in case we decided to use this directly as a string later */
3035  ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1);
3036  if(ret_val->name_raw == NULL)
[180]3037    goto fail_locked;
[99]3038
3039  /* Don't need to seek, should be at the right offset */
3040  length = ret_val->name_length;
[206]3041  if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0)
[99]3042     || length != ret_val->name_length)
3043  {
[182]3044    regfi_log_add(REGFI_LOG_ERROR, "Failed to read key name"
3045                  " while parsing NK record at offset 0x%.8X.", offset);
[180]3046    goto fail_locked;
[99]3047  }
3048
[186]3049  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_nk"))
[180]3050    goto fail;
3051
[126]3052  return ret_val;
[180]3053
3054 fail_locked:
[186]3055  regfi_unlock(file, &file->cb_lock, "regfi_parse_nk");
[180]3056 fail:
3057  talloc_free(ret_val);
3058  return NULL;
[126]3059}
3060
3061
[168]3062uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset, 
[206]3063                               uint16_t* name_length, uint32_t max_size, bool strict)
[126]3064{
[168]3065  uint8_t* ret_val = NULL;
3066  uint32_t length;
3067  uint32_t cell_length;
[126]3068  bool unalloc = false;
3069
[180]3070  if(*name_length <= 0 || offset == REGFI_OFFSET_NONE 
3071     || (offset & 0x00000007) != 0)
3072  { goto fail; }
3073
[186]3074  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_classname"))
[180]3075    goto fail;
3076
3077  if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc))
[131]3078  {
[182]3079    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
3080                  " while parsing class name at offset 0x%.8X.", offset);
[180]3081    goto fail_locked;
3082  }
3083 
3084  if((cell_length & 0x0000007) != 0)
3085  {
[182]3086    regfi_log_add(REGFI_LOG_ERROR, "Cell length not a multiple of 8"
3087                  " while parsing class name at offset 0x%.8X.", offset);
[180]3088    goto fail_locked;
3089  }
3090 
3091  if(cell_length > max_size)
3092  {
[182]3093    regfi_log_add(REGFI_LOG_WARN, "Cell stretches past hbin "
3094                  "boundary while parsing class name at offset 0x%.8X.",
3095                  offset);
[180]3096    if(strict)
3097      goto fail_locked;
3098    cell_length = max_size;
3099  }
3100 
3101  if((cell_length - 4) < *name_length)
3102  {
[182]3103    regfi_log_add(REGFI_LOG_WARN, "Class name is larger than"
3104                  " cell_length while parsing class name at offset"
3105                  " 0x%.8X.", offset);
[180]3106    if(strict)
3107      goto fail_locked;
3108    *name_length = cell_length - 4;
3109  }
3110 
3111  ret_val = talloc_array(NULL, uint8_t, *name_length);
3112  if(ret_val != NULL)
3113  {
3114    length = *name_length;
3115    if((regfi_read(file->cb, ret_val, &length) != 0)
3116       || length != *name_length)
[137]3117    {
[182]3118      regfi_log_add(REGFI_LOG_ERROR, "Could not read class name"
3119                    " while parsing class name at offset 0x%.8X.", offset);
[180]3120      goto fail_locked;
[137]3121    }
[180]3122  }
[126]3123
[186]3124  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_classname"))
[180]3125    goto fail;
[137]3126
[180]3127  return ret_val;
[131]3128
[180]3129 fail_locked:
[186]3130  regfi_unlock(file, &file->cb_lock, "regfi_parse_classname");
[180]3131 fail:
3132  talloc_free(ret_val);
3133  return NULL;
[99]3134}
3135
3136
[152]3137/******************************************************************************
3138*******************************************************************************/
[203]3139REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
[168]3140                             uint32_t max_size, bool strict)
[97]3141{
[203]3142  REGFI_VK* ret_val;
[168]3143  uint8_t vk_header[REGFI_VK_MIN_LENGTH];
3144  uint32_t raw_data_size, length, cell_length;
[101]3145  bool unalloc = false;
[97]3146
[203]3147  ret_val = talloc(NULL, REGFI_VK);
[180]3148  if(ret_val == NULL)
3149    goto fail;
3150
[186]3151  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_nk"))
[180]3152    goto fail;
3153
[178]3154  if(!regfi_parse_cell(file->cb, offset, vk_header, REGFI_VK_MIN_LENGTH,
[101]3155                       &cell_length, &unalloc))
[137]3156  {
[182]3157    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
3158                  " while parsing VK record at offset 0x%.8X.", offset);
[180]3159    goto fail_locked;
[137]3160  }
[111]3161
[101]3162  ret_val->offset = offset;
3163  ret_val->cell_size = cell_length;
[206]3164  ret_val->name = NULL;
3165  ret_val->name_raw = NULL;
[150]3166 
[101]3167  if(ret_val->cell_size > max_size)
3168    ret_val->cell_size = max_size & 0xFFFFFFF8;
3169  if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) 
[157]3170     || (ret_val->cell_size & 0x00000007) != 0)
[97]3171  {
[182]3172    regfi_log_add(REGFI_LOG_WARN, "Invalid cell size encountered"
3173                  " while parsing VK record at offset 0x%.8X.", offset);
[180]3174    goto fail_locked;
[101]3175  }
[97]3176
[101]3177  ret_val->magic[0] = vk_header[0x0];
3178  ret_val->magic[1] = vk_header[0x1];
3179  if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k'))
3180  {
[124]3181    /* XXX: This does not account for deleted keys under Win2K which
3182     *      often have this (and the name length) overwritten with
3183     *      0xFFFF.
3184     */
[182]3185    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch"
3186                  " while parsing VK record at offset 0x%.8X.", offset);
[180]3187    goto fail_locked;
[101]3188  }
3189
3190  ret_val->name_length = SVAL(vk_header, 0x2);
3191  raw_data_size = IVAL(vk_header, 0x4);
[135]3192  ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET;
[157]3193  /* The data is typically stored in the offset if the size <= 4,
3194   * in which case this flag is set.
3195   */
[135]3196  ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET);
[101]3197  ret_val->data_off = IVAL(vk_header, 0x8);
3198  ret_val->type = IVAL(vk_header, 0xC);
[162]3199  ret_val->flags = SVAL(vk_header, 0x10);
[101]3200  ret_val->unknown1 = SVAL(vk_header, 0x12);
3201
[162]3202  if(ret_val->name_length > 0)
[101]3203  {
[113]3204    if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size)
[101]3205    {
[182]3206      regfi_log_add(REGFI_LOG_WARN, "Name too long for remaining cell"
3207                    " space while parsing VK record at offset 0x%.8X.",
3208                    offset);
[101]3209      if(strict)
[180]3210        goto fail_locked;
[101]3211      else
[113]3212        ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4;
[101]3213    }
3214
3215    /* Round up to the next multiple of 8 */
[113]3216    cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8;
3217    if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4)
3218      cell_length+=8;
[101]3219
[206]3220    /* +1 to length in case we decided to use this directly as a string later */
3221    ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1);
3222    if(ret_val->name_raw == NULL)
[180]3223      goto fail_locked;
[113]3224
[101]3225    length = ret_val->name_length;
[206]3226    if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0)
[101]3227       || length != ret_val->name_length)
3228    {
[182]3229      regfi_log_add(REGFI_LOG_ERROR, "Could not read value name"
3230                    " while parsing VK record at offset 0x%.8X.", offset);
[180]3231      goto fail_locked;
[101]3232    }
3233  }
3234  else
[113]3235    cell_length = REGFI_VK_MIN_LENGTH + 4;
[101]3236
[186]3237  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_nk"))
[180]3238    goto fail;
3239
[101]3240  if(unalloc)
3241  {
3242    /* If cell_size is still greater, truncate. */
[113]3243    if(cell_length < ret_val->cell_size)
3244      ret_val->cell_size = cell_length;
[101]3245  }
3246
3247  return ret_val;
[180]3248 
3249 fail_locked:
[186]3250  regfi_unlock(file, &file->cb_lock, "regfi_parse_vk");
[180]3251 fail:
3252  talloc_free(ret_val);
3253  return NULL;
[97]3254}
[101]3255
3256
[152]3257/******************************************************************************
[157]3258 *
3259 ******************************************************************************/
[168]3260REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset,
3261                             uint32_t length, bool data_in_offset,
[157]3262                             bool strict)
[101]3263{
[151]3264  REGFI_BUFFER ret_val;
[168]3265  uint32_t cell_length, offset;
3266  int32_t max_size;
[101]3267  bool unalloc;
[151]3268 
[159]3269  /* Microsoft's documentation indicates that "available memory" is
[165]3270   * the limit on value sizes for the more recent registry format version.
3271   * This is not only annoying, but it's probably also incorrect, since clearly
3272   * value data sizes are limited to 2^31 (high bit used as a flag) and even
3273   * with big data records, the apparent max size is:
3274   *   16344 * 2^16 = 1071104040 (~1GB).
3275   *
3276   * We choose to limit it to 1M which was the limit in older versions and
3277   * should rarely be exceeded unless the file is corrupt or malicious.
3278   * For more info, see:
3279   *   http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
[159]3280   */
[160]3281  /* XXX: add way to skip this check at user discression. */
3282  if(length > REGFI_VK_MAX_DATA_LENGTH)
[159]3283  {
[182]3284    regfi_log_add(REGFI_LOG_WARN, "Value data size %d larger than "
3285                  "%d, truncating...", length, REGFI_VK_MAX_DATA_LENGTH);
[160]3286    length = REGFI_VK_MAX_DATA_LENGTH;
[159]3287  }
3288
[145]3289  if(data_in_offset)
[157]3290    return regfi_parse_little_data(file, voffset, length, strict);
3291  else
[101]3292  {
[157]3293    offset = voffset + REGFI_REGF_SIZE;
3294    max_size = regfi_calc_maxsize(file, offset);
3295    if(max_size < 0)
[137]3296    {
[182]3297      regfi_log_add(REGFI_LOG_WARN, "Could not find HBIN for data"
3298                    " at offset 0x%.8X.", offset);
[151]3299      goto fail;
[137]3300    }
[157]3301   
[186]3302    if(!regfi_lock(file, &file->cb_lock, "regfi_load_data"))
[180]3303      goto fail;
3304
[178]3305    if(!regfi_parse_cell(file->cb, offset, NULL, 0,
[101]3306                         &cell_length, &unalloc))
[137]3307    {
[182]3308      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3309                    " parsing data record at offset 0x%.8X.", offset);
[180]3310      goto fail_locked;
[137]3311    }
[111]3312
[186]3313    if(!regfi_unlock(file, &file->cb_lock, "regfi_load_data"))
[180]3314      goto fail;
3315
[157]3316    if((cell_length & 0x00000007) != 0)
[137]3317    {
[182]3318      regfi_log_add(REGFI_LOG_WARN, "Cell length not multiple of 8"
3319                    " while parsing data record at offset 0x%.8X.",
3320                    offset);
[151]3321      goto fail;
[137]3322    }
[101]3323
[131]3324    if(cell_length > max_size)
3325    {
[182]3326      regfi_log_add(REGFI_LOG_WARN, "Cell extends past HBIN boundary"
3327                    " while parsing data record at offset 0x%.8X.",
3328                    offset);
[157]3329      goto fail;
[131]3330    }
3331
[101]3332    if(cell_length - 4 < length)
3333    {
[155]3334      /* XXX: All big data records thus far have been 16 bytes long. 
3335       *      Should we check for this precise size instead of just
3336       *      relying upon the above check?
3337       */
[152]3338      if (file->major_version >= 1 && file->minor_version >= 5)
3339      {
3340        /* Attempt to parse a big data record */
[157]3341        return regfi_load_big_data(file, offset, length, cell_length, 
3342                                   NULL, strict);
[152]3343      }
[101]3344      else
[152]3345      {
[182]3346        regfi_log_add(REGFI_LOG_WARN, "Data length (0x%.8X) larger than"
3347                      " remaining cell length (0x%.8X)"
3348                      " while parsing data record at offset 0x%.8X.", 
3349                      length, cell_length - 4, offset);
[152]3350        if(strict)
3351          goto fail;
3352        else
3353          length = cell_length - 4;
3354      }
[101]3355    }
3356
[157]3357    ret_val = regfi_parse_data(file, offset, length, strict);
[101]3358  }
3359
3360  return ret_val;
[151]3361
[180]3362 fail_locked:
[186]3363  regfi_unlock(file, &file->cb_lock, "regfi_load_data");
[151]3364 fail:
3365  ret_val.buf = NULL;
3366  ret_val.len = 0;
3367  return ret_val;
[101]3368}
[110]3369
3370
[152]3371/******************************************************************************
[157]3372 * Parses the common case data records stored in a single cell.
3373 ******************************************************************************/
[168]3374REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset,
3375                              uint32_t length, bool strict)
[157]3376{
3377  REGFI_BUFFER ret_val;
[168]3378  uint32_t read_length;
[157]3379
3380  ret_val.buf = NULL;
3381  ret_val.len = 0;
3382 
[180]3383  if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL)
3384    goto fail;
3385  ret_val.len = length;
3386
[186]3387  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_data"))
[180]3388    goto fail;
3389
[178]3390  if(regfi_seek(file->cb, offset+4, SEEK_SET) == -1)
[157]3391  {
[182]3392    regfi_log_add(REGFI_LOG_WARN, "Could not seek while "
3393                  "reading data at offset 0x%.8X.", offset);
[180]3394    goto fail_locked;
[157]3395  }
3396 
3397  read_length = length;
[178]3398  if((regfi_read(file->cb, ret_val.buf, &read_length) != 0)
[157]3399     || read_length != length)
3400  {
[182]3401    regfi_log_add(REGFI_LOG_ERROR, "Could not read data block while"
3402                  " parsing data record at offset 0x%.8X.", offset);
[180]3403    goto fail_locked;
[157]3404  }
3405
[186]3406  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_data"))
[180]3407    goto fail;
3408
[157]3409  return ret_val;
[180]3410
3411 fail_locked:
[186]3412  regfi_unlock(file, &file->cb_lock, "regfi_parse_data");
[180]3413 fail:
3414  talloc_free(ret_val.buf);
3415  ret_val.buf = NULL;
3416  ret_val.buf = 0;
3417  return ret_val;
[157]3418}
3419
3420
3421
3422/******************************************************************************
3423 *
3424 ******************************************************************************/
[168]3425REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset,
3426                                     uint32_t length, bool strict)
[157]3427{
[173]3428  uint8_t i;
[157]3429  REGFI_BUFFER ret_val;
3430
3431  ret_val.buf = NULL;
3432  ret_val.len = 0;
3433
3434  if(length > 4)
3435  {
[182]3436    regfi_log_add(REGFI_LOG_ERROR, "Data in offset but length > 4"
3437                  " while parsing data record. (voffset=0x%.8X, length=%d)",
3438                  voffset, length);
[157]3439    return ret_val;
3440  }
3441
[168]3442  if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL)
[157]3443    return ret_val;
3444  ret_val.len = length;
3445 
3446  for(i = 0; i < length; i++)
[168]3447    ret_val.buf[i] = (uint8_t)((voffset >> i*8) & 0xFF);
[157]3448
3449  return ret_val;
3450}
3451
3452/******************************************************************************
[152]3453*******************************************************************************/
[168]3454REGFI_BUFFER regfi_parse_big_data_header(REGFI_FILE* file, uint32_t offset, 
3455                                         uint32_t max_size, bool strict)
[152]3456{
3457  REGFI_BUFFER ret_val;
[168]3458  uint32_t cell_length;
[152]3459  bool unalloc;
[157]3460
3461  /* XXX: do something with unalloc? */
[168]3462  ret_val.buf = (uint8_t*)talloc_array(NULL, uint8_t, REGFI_BIG_DATA_MIN_LENGTH);
[157]3463  if(ret_val.buf == NULL)
[152]3464    goto fail;
3465
[157]3466  if(REGFI_BIG_DATA_MIN_LENGTH > max_size)
3467  {
[182]3468    regfi_log_add(REGFI_LOG_WARN, "Big data header exceeded max_size "
3469                  "while parsing big data header at offset 0x%.8X.",offset);
[157]3470    goto fail;
3471  }
3472
[186]3473  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_header"))
[180]3474    goto fail;
3475
3476
[178]3477  if(!regfi_parse_cell(file->cb, offset, ret_val.buf, REGFI_BIG_DATA_MIN_LENGTH,
[152]3478                       &cell_length, &unalloc))
3479  {
[182]3480    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3481                  " parsing big data header at offset 0x%.8X.", offset);
[180]3482    goto fail_locked;
[152]3483  }
[157]3484
[186]3485  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_header"))
[180]3486    goto fail;
3487
[157]3488  if((ret_val.buf[0] != 'd') || (ret_val.buf[1] != 'b'))
[152]3489  {
[182]3490    regfi_log_add(REGFI_LOG_WARN, "Unknown magic number"
3491                  " (0x%.2X, 0x%.2X) encountered while parsing"
3492                  " big data header at offset 0x%.8X.", 
3493                  ret_val.buf[0], ret_val.buf[1], offset);
[152]3494    goto fail;
3495  }
3496
[157]3497  ret_val.len = REGFI_BIG_DATA_MIN_LENGTH;
3498  return ret_val;
3499
[180]3500 fail_locked:
[186]3501  regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_header");
[157]3502 fail:
[180]3503  talloc_free(ret_val.buf);
3504  ret_val.buf = NULL;
[157]3505  ret_val.len = 0;
3506  return ret_val;
3507}
3508
3509
3510
3511/******************************************************************************
3512 *
3513 ******************************************************************************/
[168]3514uint32_t* regfi_parse_big_data_indirect(REGFI_FILE* file, uint32_t offset,
3515                                      uint16_t num_chunks, bool strict)
[157]3516{
[168]3517  uint32_t* ret_val;
3518  uint32_t indirect_length;
3519  int32_t max_size;
3520  uint16_t i;
[157]3521  bool unalloc;
3522
3523  /* XXX: do something with unalloc? */
3524  max_size = regfi_calc_maxsize(file, offset);
[168]3525  if((max_size < 0) || (num_chunks*sizeof(uint32_t) + 4 > max_size))
[157]3526    return NULL;
3527
[168]3528  ret_val = (uint32_t*)talloc_array(NULL, uint32_t, num_chunks);
[157]3529  if(ret_val == NULL)
[152]3530    goto fail;
3531
[186]3532  if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_indirect"))
[180]3533    goto fail;
3534
[178]3535  if(!regfi_parse_cell(file->cb, offset, (uint8_t*)ret_val,
[168]3536                       num_chunks*sizeof(uint32_t),
[152]3537                       &indirect_length, &unalloc))
3538  {
[182]3539    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3540                  " parsing big data indirect record at offset 0x%.8X.", 
3541                  offset);
[180]3542    goto fail_locked;
[152]3543  }
[157]3544
[186]3545  if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_indirect"))
[180]3546    goto fail;
3547
[157]3548  /* Convert pointers to proper endianess, verify they are aligned. */
3549  for(i=0; i<num_chunks; i++)
[152]3550  {
[168]3551    ret_val[i] = IVAL(ret_val, i*sizeof(uint32_t));
[157]3552    if((ret_val[i] & 0x00000007) != 0)
3553      goto fail;
[152]3554  }
[157]3555 
3556  return ret_val;
[152]3557
[180]3558 fail_locked:
[186]3559  regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_indirect");
[157]3560 fail:
[180]3561  talloc_free(ret_val);
[157]3562  return NULL;
3563}
3564
3565
3566/******************************************************************************
3567 * Arguments:
3568 *  file       --
3569 *  offsets    -- list of virtual offsets.
3570 *  num_chunks --
3571 *  strict     --
3572 *
3573 * Returns:
3574 *  A range_list with physical offsets and complete lengths
3575 *  (including cell headers) of associated cells. 
3576 *  No data in range_list elements.
3577 ******************************************************************************/
[168]3578range_list* regfi_parse_big_data_cells(REGFI_FILE* file, uint32_t* offsets,
3579                                       uint16_t num_chunks, bool strict)
[157]3580{
[168]3581  uint32_t cell_length, chunk_offset;
[157]3582  range_list* ret_val;
[168]3583  uint16_t i;
[157]3584  bool unalloc;
3585 
3586  /* XXX: do something with unalloc? */
3587  ret_val = range_list_new();
3588  if(ret_val == NULL)
3589    goto fail;
3590 
[166]3591  for(i=0; i<num_chunks; i++)
[152]3592  {
[186]3593    if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_cells"))
[180]3594      goto fail;
3595
[157]3596    chunk_offset = offsets[i]+REGFI_REGF_SIZE;
[178]3597    if(!regfi_parse_cell(file->cb, chunk_offset, NULL, 0,
[157]3598                         &cell_length, &unalloc))
[152]3599    {
[182]3600      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
3601                    " parsing big data chunk at offset 0x%.8X.", 
3602                    chunk_offset);
[180]3603      goto fail_locked;
[152]3604    }
3605
[186]3606    if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_cells"))
[180]3607      goto fail;
3608
[157]3609    if(!range_list_add(ret_val, chunk_offset, cell_length, NULL))
3610      goto fail;
3611  }
3612
3613  return ret_val;
3614
[180]3615 fail_locked:
[186]3616  regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_cells");
[157]3617 fail:
3618  if(ret_val != NULL)
3619    range_list_free(ret_val);
3620  return NULL;
3621}
3622
3623
3624/******************************************************************************
3625*******************************************************************************/
3626REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, 
[168]3627                                 uint32_t offset, uint32_t data_length, 
3628                                 uint32_t cell_length, range_list* used_ranges,
[157]3629                                 bool strict)
3630{
3631  REGFI_BUFFER ret_val;
[168]3632  uint16_t num_chunks, i;
3633  uint32_t read_length, data_left, tmp_len, indirect_offset;
3634  uint32_t* indirect_ptrs = NULL;
[157]3635  REGFI_BUFFER bd_header;
3636  range_list* bd_cells = NULL;
3637  const range_list_element* cell_info;
3638
3639  ret_val.buf = NULL;
3640
3641  /* XXX: Add better error/warning messages */
3642
3643  bd_header = regfi_parse_big_data_header(file, offset, cell_length, strict);
3644  if(bd_header.buf == NULL)
3645    goto fail;
3646
3647  /* Keep track of used space for use by reglookup-recover */
3648  if(used_ranges != NULL)
3649    if(!range_list_add(used_ranges, offset, cell_length, NULL))
3650      goto fail;
3651
3652  num_chunks = SVAL(bd_header.buf, 0x2);
3653  indirect_offset = IVAL(bd_header.buf, 0x4) + REGFI_REGF_SIZE;
3654  talloc_free(bd_header.buf);
3655
3656  indirect_ptrs = regfi_parse_big_data_indirect(file, indirect_offset,
3657                                                num_chunks, strict);
3658  if(indirect_ptrs == NULL)
3659    goto fail;
3660
3661  if(used_ranges != NULL)
3662    if(!range_list_add(used_ranges, indirect_offset, num_chunks*4+4, NULL))
3663      goto fail;
3664 
3665  if((ret_val.buf = talloc_array(NULL, uint8_t, data_length)) == NULL)
3666    goto fail;
3667  data_left = data_length;
3668
3669  bd_cells = regfi_parse_big_data_cells(file, indirect_ptrs, num_chunks, strict);
3670  if(bd_cells == NULL)
3671    goto fail;
3672
3673  talloc_free(indirect_ptrs);
3674  indirect_ptrs = NULL;
3675 
3676  for(i=0; (i<num_chunks) && (data_left>0); i++)
3677  {
3678    cell_info = range_list_get(bd_cells, i);
3679    if(cell_info == NULL)
3680      goto fail;
3681
3682    /* XXX: This should be "cell_info->length-4" to account for the 4 byte cell
[154]3683     *      length.  However, it has been observed that some (all?) chunks
3684     *      have an additional 4 bytes of 0 at the end of their cells that
3685     *      isn't part of the data, so we're trimming that off too.
[157]3686     *      Perhaps it's just an 8 byte alignment requirement...
[154]3687     */
[157]3688    if(cell_info->length - 8 >= data_left)
3689    {
3690      if(i+1 != num_chunks)
3691      {
[182]3692        regfi_log_add(REGFI_LOG_WARN, "Left over chunks detected "
3693                      "while constructing big data at offset 0x%.8X "
3694                      "(chunk offset 0x%.8X).", offset, cell_info->offset);
[157]3695      }
[152]3696      read_length = data_left;
[157]3697    }
[152]3698    else
[157]3699      read_length = cell_info->length - 8;
[152]3700
[157]3701
3702    if(read_length > regfi_calc_maxsize(file, cell_info->offset))
3703    {
[182]3704      regfi_log_add(REGFI_LOG_WARN, "A chunk exceeded the maxsize "
3705                    "while constructing big data at offset 0x%.8X "
3706                    "(chunk offset 0x%.8X).", offset, cell_info->offset);
[157]3707      goto fail;
3708    }
3709
[186]3710    if(!regfi_lock(file, &file->cb_lock, "regfi_load_big_data"))
[180]3711      goto fail;
3712
[178]3713    if(regfi_seek(file->cb, cell_info->offset+sizeof(uint32_t), SEEK_SET) == -1)
[157]3714    {
[182]3715      regfi_log_add(REGFI_LOG_WARN, "Could not seek to chunk while "
3716                    "constructing big data at offset 0x%.8X "
3717                    "(chunk offset 0x%.8X).", offset, cell_info->offset);
[180]3718      goto fail_locked;
[157]3719    }
3720
3721    tmp_len = read_length;
[178]3722    if(regfi_read(file->cb, ret_val.buf+(data_length-data_left), 
[157]3723                  &read_length) != 0 || (read_length != tmp_len))
[152]3724    {
[182]3725      regfi_log_add(REGFI_LOG_WARN, "Could not read data chunk while"
3726                    " constructing big data at offset 0x%.8X"
3727                    " (chunk offset 0x%.8X).", offset, cell_info->offset);
[180]3728      goto fail_locked;
[152]3729    }
3730
[186]3731    if(!regfi_unlock(file, &file->cb_lock, "regfi_load_big_data"))
[180]3732      goto fail;
3733
[157]3734    if(used_ranges != NULL)
3735      if(!range_list_add(used_ranges, cell_info->offset,cell_info->length,NULL))
3736        goto fail;
3737
[152]3738    data_left -= read_length;
3739  }
[157]3740  range_list_free(bd_cells);
3741
[152]3742  ret_val.len = data_length-data_left;
3743  return ret_val;
3744
[180]3745 fail_locked:
[186]3746  regfi_unlock(file, &file->cb_lock, "regfi_load_big_data");
[152]3747 fail:
[180]3748  talloc_free(ret_val.buf);
3749  talloc_free(indirect_ptrs);
[157]3750  if(bd_cells != NULL)
3751    range_list_free(bd_cells);
[152]3752  ret_val.buf = NULL;
3753  ret_val.len = 0;
3754  return ret_val;
3755}
3756
3757
[135]3758range_list* regfi_parse_unalloc_cells(REGFI_FILE* file)
[110]3759{
3760  range_list* ret_val;
[135]3761  REGFI_HBIN* hbin;
[110]3762  const range_list_element* hbins_elem;
[168]3763  uint32_t i, num_hbins, curr_off, cell_len;
[110]3764  bool is_unalloc;
3765
3766  ret_val = range_list_new();
3767  if(ret_val == NULL)
3768    return NULL;
3769
[186]3770  if(!regfi_read_lock(file, &file->hbins_lock, "regfi_parse_unalloc_cells"))
[180]3771  {
3772    range_list_free(ret_val);
3773    return NULL;
3774  }
3775
[110]3776  num_hbins = range_list_size(file->hbins);
3777  for(i=0; i<num_hbins; i++)
3778  {
3779    hbins_elem = range_list_get(file->hbins, i);
3780    if(hbins_elem == NULL)
3781      break;
[135]3782    hbin = (REGFI_HBIN*)hbins_elem->data;
[110]3783
[135]3784    curr_off = REGFI_HBIN_HEADER_SIZE;
[110]3785    while(curr_off < hbin->block_size)
3786    {
[186]3787      if(!regfi_lock(file, &file->cb_lock, "regfi_parse_unalloc_cells"))
[180]3788        break;
3789
[178]3790      if(!regfi_parse_cell(file->cb, hbin->file_off+curr_off, NULL, 0,
[110]3791                           &cell_len, &is_unalloc))
[180]3792      {
[186]3793        regfi_unlock(file, &file->cb_lock, "regfi_parse_unalloc_cells");
[110]3794        break;
[180]3795      }
3796
[186]3797      if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_unalloc_cells"))
[180]3798        break;
3799
[157]3800      if((cell_len == 0) || ((cell_len & 0x00000007) != 0))
[140]3801      {
[182]3802        regfi_log_add(REGFI_LOG_ERROR, "Bad cell length encountered"
3803                      " while parsing unallocated cells at offset 0x%.8X.",
3804                      hbin->file_off+curr_off);
[110]3805        break;
[140]3806      }
3807
[110]3808      /* for some reason the record_size of the last record in
3809         an hbin block can extend past the end of the block
3810         even though the record fits within the remaining
3811         space....aaarrrgggghhhhhh */ 
3812      if(curr_off + cell_len >= hbin->block_size)
3813        cell_len = hbin->block_size - curr_off;
3814     
3815      if(is_unalloc)
3816        range_list_add(ret_val, hbin->file_off+curr_off, 
3817                       cell_len, NULL);
3818     
3819      curr_off = curr_off+cell_len;
3820    }
3821  }
3822
[186]3823  if(!regfi_rw_unlock(file, &file->hbins_lock, "regfi_parse_unalloc_cells"))
[180]3824  {
3825    range_list_free(ret_val);
3826    return NULL;
3827  }
3828
[110]3829  return ret_val;
3830}
[168]3831
3832
3833/* From lib/time.c */
3834
3835/****************************************************************************
[251]3836 Returns an 8 byte filetime from a time_t
[168]3837 This takes real GMT as input and converts to kludge-GMT
3838****************************************************************************/
[251]3839REGFI_NTTIME regfi_unix2nt_time(time_t t)
[168]3840{
3841  double d;
[251]3842
3843  if (t==0)
3844    return 0L;
[168]3845 
3846  if (t == TIME_T_MAX) 
[251]3847    return 0x7fffffffffffffffL;
[168]3848 
3849  if (t == -1) 
[251]3850    return 0xffffffffffffffffL;
[168]3851 
3852  /* this converts GMT to kludge-GMT */
3853  /* XXX: This was removed due to difficult dependency requirements. 
3854   *      So far, times appear to be correct without this adjustment, but
3855   *      that may be proven wrong with adequate testing.
3856   */
3857  /* t -= TimeDiff(t) - get_serverzone(); */
3858 
[251]3859  d = (double)(t) + REGFI_TIME_FIXUP;
[168]3860  d *= 1.0e7;
[251]3861  /*
3862  nt->high = (uint32_t)(d * (1.0/c));
3863  nt->low  = (uint32_t)(d - ((double)nt->high) * c);
3864  */
3865
3866  return (REGFI_NTTIME) d;
[168]3867}
3868
3869
3870/****************************************************************************
3871 Interpret an 8 byte "filetime" structure to a time_t
3872 It's originally in "100ns units since jan 1st 1601"
3873
3874 An 8 byte value of 0xffffffffffffffff will be returned as (time_t)0.
3875
3876 It appears to be kludge-GMT (at least for file listings). This means
3877 its the GMT you get by taking a localtime and adding the
3878 serverzone. This is NOT the same as GMT in some cases. This routine
3879 converts this to real GMT.
3880****************************************************************************/
[251]3881double regfi_nt2unix_time(REGFI_NTTIME nt)
[168]3882{
[219]3883  double ret_val;
[168]3884 
[251]3885  if (nt == 0 || nt == 0xffffffffffffffffL)
3886    return 0;
[168]3887 
[251]3888  ret_val = (double)(nt) * 1.0e-7;
[168]3889 
3890  /* now adjust by 369 years to make the secs since 1970 */
[251]3891  ret_val -= REGFI_TIME_FIXUP;
[168]3892 
3893  /* this takes us from kludge-GMT to real GMT */
3894  /* XXX: This was removed due to difficult dependency requirements. 
3895   *      So far, times appear to be correct without this adjustment, but
3896   *      that may be proven wrong with adequate testing.
3897   */
3898  /*
3899    ret -= get_serverzone();
3900    ret += LocTimeDiff(ret);
3901  */
3902
[219]3903  return ret_val;
[168]3904}
3905
3906/* End of stuff from lib/time.c */
Note: See TracBrowser for help on using the repository browser.