Changeset 207 for trunk/lib/regfi.c


Ignore:
Timestamp:
09/06/10 23:03:36 (14 years ago)
Author:
tim
Message:

reworked regfi API to accomodate more convenient subkey/value searches without an iterator.

continued implementation on pyregfi

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/regfi.c

    r206 r207  
    17211721
    17221722
     1723
     1724/******************************************************************************
     1725 *****************************************************************************/
     1726uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key)
     1727{
     1728  uint32_t num_in_list = 0;
     1729  if(key->subkeys != NULL)
     1730    num_in_list = key->subkeys->num_keys;
     1731
     1732  if(num_in_list != key->num_subkeys)
     1733  {
     1734    regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d keys in its"
     1735                  " subkey list but reports %d should be available.",
     1736                  key->offset, num_in_list, key->num_subkeys);
     1737    return (num_in_list < key->num_subkeys)?num_in_list:key->num_subkeys;
     1738  }
     1739 
     1740  return num_in_list;
     1741}
     1742
     1743
     1744/******************************************************************************
     1745 *****************************************************************************/
     1746uint32_t regfi_fetch_num_values(const REGFI_NK* key)
     1747{
     1748  uint32_t num_in_list = 0;
     1749  if(key->values != NULL)
     1750    num_in_list = key->values->num_values;
     1751
     1752  if(num_in_list != key->num_values)
     1753  {
     1754    regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d values in"
     1755                  " its value list but reports %d should be available.",
     1756                  key->offset, num_in_list, key->num_values);
     1757    return (num_in_list < key->num_values)?num_in_list:key->num_values;
     1758  }
     1759 
     1760  return num_in_list;
     1761}
     1762
     1763
    17231764/******************************************************************************
    17241765 *****************************************************************************/
     
    18361877/******************************************************************************
    18371878 *****************************************************************************/
    1838 bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name)
    1839 {
    1840   REGFI_NK* subkey;
    1841   bool found = false;
    1842   uint32_t old_subkey = i->cur_subkey;
    1843 
    1844   if(subkey_name == NULL)
    1845     return false;
    1846 
    1847   /* XXX: this alloc/free of each sub key might be a bit excessive */
    1848   regfi_iterator_first_subkey(i);
    1849   while((subkey = regfi_iterator_cur_subkey(i)) != NULL && (found == false))
    1850   {
    1851     if(subkey->name != NULL
    1852        && strcasecmp(subkey->name, subkey_name) == 0)
    1853       found = true;
    1854     else
    1855     {
    1856       talloc_unlink(NULL, subkey);
    1857       regfi_iterator_next_subkey(i);
    1858     }
    1859   }
    1860 
    1861   if(found == false)
    1862   {
    1863     i->cur_subkey = old_subkey;
    1864     return false;
    1865   }
    1866 
    1867   talloc_unlink(NULL, subkey);
    1868   return true;
     1879bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name)
     1880{
     1881  uint32_t new_index;
     1882
     1883  if(regfi_find_subkey(i->f, i->cur_key, name, &new_index))
     1884  {
     1885    i->cur_subkey = new_index;
     1886    return true;
     1887  }
     1888
     1889  return false;
    18691890}
    18701891
     
    19221943 
    19231944  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
    1924           && (i->cur_subkey < i->cur_key->num_subkeys));
     1945          && (i->cur_subkey < regfi_fetch_num_subkeys(i->cur_key)));
    19251946}
    19261947
     
    19301951const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i)
    19311952{
    1932   uint32_t nk_offset;
    1933 
    1934   if((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
    1935      && (i->cur_subkey < i->cur_key->num_subkeys))
    1936   {
    1937     nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
    1938 
    1939     return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE,
    1940                           i->f->string_encoding, true);
    1941   }
    1942 
    1943   return NULL;
     1953  return regfi_get_subkey(i->f, i->cur_key, i->cur_subkey);
    19441954}
    19451955
     
    19521962
    19531963  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
    1954           && (i->cur_subkey < i->cur_key->num_subkeys));
     1964          && (i->cur_subkey < regfi_fetch_num_subkeys(i->cur_key)));
    19551965}
    19561966
     
    19581968/******************************************************************************
    19591969 *****************************************************************************/
    1960 bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name)
    1961 {
    1962   const REGFI_VK* cur;
    1963   bool found = false;
    1964   uint32_t old_value = i->cur_value;
    1965 
    1966   /* XXX: cur->name can be NULL in the registry. 
    1967    *      Should we allow for a way to search for that?
    1968    */
    1969   if(value_name == NULL)
    1970     return false;
    1971 
    1972   regfi_iterator_first_value(i);
    1973   while((cur = regfi_iterator_cur_value(i)) != NULL && (found == false))
    1974   {
    1975     if((cur->name != NULL)
    1976        && (strcasecmp(cur->name, value_name) == 0))
    1977       found = true;
    1978     else
    1979     {
    1980       regfi_free_record(cur);
    1981       regfi_iterator_next_value(i);
    1982     }
    1983   }
    1984  
    1985   if(found == false)
    1986   {
    1987     i->cur_value = old_value;
    1988     return false;
    1989   }
    1990 
    1991   regfi_free_record(cur);
    1992   return true;
     1970bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name)
     1971{
     1972  uint32_t new_index;
     1973
     1974  if(regfi_find_value(i->f, i->cur_key, name, &new_index))
     1975  {
     1976    i->cur_value = new_index;
     1977    return true;
     1978  }
     1979
     1980  return false;
    19931981}
    19941982
     
    20001988  i->cur_value = 0;
    20011989  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL
    2002           && (i->cur_value < i->cur_key->values->num_values));
     1990          && (i->cur_value < regfi_fetch_num_values(i->cur_key)));
    20031991}
    20041992
     
    20081996const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i)
    20091997{
    2010   REGFI_VK* ret_val = NULL;
    2011   uint32_t voffset;
    2012 
    2013   if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL
    2014      && (i->cur_value < i->cur_key->values->num_values))
    2015   {
    2016     voffset = i->cur_key->values->elements[i->cur_value];
    2017     ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE,
    2018                                i->f->string_encoding, true);
    2019   }
    2020 
    2021   return ret_val;
     1998  return regfi_get_value(i->f, i->cur_key, i->cur_value);
    20221999}
    20232000
     
    20292006  i->cur_value++;
    20302007  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL
    2031           && (i->cur_value < i->cur_key->values->num_values));
     2008          && (i->cur_value < regfi_fetch_num_values(i->cur_key)));
    20322009}
    20332010
     
    21412118 
    21422119  return ret_val;
     2120}
     2121
     2122
     2123
     2124/******************************************************************************
     2125 *****************************************************************************/
     2126bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key,
     2127                       const char* name, uint32_t* index)
     2128{
     2129  const REGFI_NK* cur;
     2130  uint32_t i;
     2131  uint32_t num_subkeys = regfi_fetch_num_subkeys(key);
     2132  bool found = false;
     2133
     2134  /* XXX: cur->name can be NULL in the registry. 
     2135   *      Should we allow for a way to search for that?
     2136   */
     2137  if(name == NULL)
     2138    return false;
     2139
     2140  for(i=0; (i < num_subkeys) && (found == false); i++)
     2141  {
     2142    cur = regfi_get_subkey(file, key, i);
     2143    if(cur == NULL)
     2144      return false;
     2145
     2146    if((cur->name != NULL)
     2147       && (strcasecmp(cur->name, name) == 0))
     2148    {
     2149      found = true;
     2150      *index = i;
     2151    }
     2152
     2153    regfi_free_record(cur);
     2154  }
     2155
     2156  return found;
     2157}
     2158
     2159
     2160
     2161/******************************************************************************
     2162 *****************************************************************************/
     2163bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key,
     2164                      const char* name, uint32_t* index)
     2165{
     2166  const REGFI_VK* cur;
     2167  uint32_t i;
     2168  uint32_t num_values = regfi_fetch_num_values(key);
     2169  bool found = false;
     2170
     2171  /* XXX: cur->name can be NULL in the registry. 
     2172   *      Should we allow for a way to search for that?
     2173   */
     2174  if(name == NULL)
     2175    return false;
     2176
     2177  for(i=0; (i < num_values) && (found == false); i++)
     2178  {
     2179    cur = regfi_get_value(file, key, i);
     2180    if(cur == NULL)
     2181      return false;
     2182
     2183    if((cur->name != NULL)
     2184       && (strcasecmp(cur->name, name) == 0))
     2185    {
     2186      found = true;
     2187      *index = i;
     2188    }
     2189
     2190    regfi_free_record(cur);
     2191  }
     2192
     2193  return found;
     2194}
     2195
     2196
     2197
     2198/******************************************************************************
     2199 *****************************************************************************/
     2200const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key,
     2201                                 uint32_t index)
     2202{
     2203  if(index < regfi_fetch_num_subkeys(key))
     2204  {
     2205    return regfi_load_key(file,
     2206                          key->subkeys->elements[index].offset+REGFI_REGF_SIZE,
     2207                          file->string_encoding, true);
     2208  }
     2209
     2210  return NULL;
     2211}
     2212
     2213
     2214
     2215/******************************************************************************
     2216 *****************************************************************************/
     2217const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key,
     2218                                uint32_t index)
     2219{
     2220  if(index < regfi_fetch_num_values(key))
     2221  {
     2222    return regfi_load_value(file,
     2223                            key->values->elements[index]+REGFI_REGF_SIZE,
     2224                            file->string_encoding, true);
     2225  }
     2226
     2227  return NULL; 
    21432228}
    21442229
Note: See TracChangeset for help on using the changeset viewer.