source: trunk/lib/regfi.c @ 272

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

fixed name/flags bug
improved error reporting on bad key/value names

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