Changeset 32 for trunk/lib


Ignore:
Timestamp:
07/16/05 19:16:17 (19 years ago)
Author:
tim
Message:

Added key name retrieval to test utility

Code formatting changes to regfio.c

Added type<->string mapping functions to regfio library

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/regfio.c

    r31 r32  
    3535 ******************************************************************/
    3636
     37/* Registry types mapping */
     38const VAL_STR reg_type_names[] =
     39{
     40  { REG_SZ,                        "SZ"           },
     41  { REG_EXPAND_SZ,                 "EXPAND_SZ"    },
     42  { REG_BINARY,                    "BIN"          },
     43  { REG_DWORD,                     "DWORD"        },
     44  { REG_DWORD_BE,                  "DWORD_BE"     },
     45  { REG_LINK,                      "LINK"         },
     46  { REG_MULTI_SZ,                  "MULTI_SZ"     },
     47  { REG_RESOURCE_LIST,             "RSRC_LIST"    },
     48  { REG_FULL_RESOURCE_DESCRIPTOR,  "RSRC_DESC"    },
     49  { REG_RESOURCE_REQUIREMENTS_LIST,"RSRC_REQ_LIST"},
     50  { REG_KEY,                       "KEY"          },
     51  { 0,                             NULL           },
     52};
     53
     54
     55/* Returns NULL on error */
     56const char* type_val2str(unsigned int val)
     57{
     58  int i;
     59
     60  for(i=0; reg_type_names[i].val && reg_type_names[i].str; i++)
     61    if (reg_type_names[i].val == val)
     62      return reg_type_names[i].str;
     63
     64  return NULL;
     65}
     66
     67
     68/* Returns 0 on error */
     69int type_str2val(const char* str)
     70{
     71  int i;
     72
     73  for(i=0; reg_type_names[i].val && reg_type_names[i].str; i++)
     74    if (strcmp(reg_type_names[i].str, str) == 0)
     75      return reg_type_names[i].val;
     76
     77  return 0;
     78}
     79
    3780
    3881/*******************************************************************
     
    95138  bytes_read = returned = 0;
    96139
    97   while ( bytes_read < block_size ) {
    98     if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
     140  while ( bytes_read < block_size )
     141  {
     142    if((returned =
     143        read(file->fd, buffer+bytes_read, block_size-bytes_read)) == -1)
     144    {
    99145      /*DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));*/
    100146      return false;
    101147    }
    102     if ( (returned == 0) && (bytes_read < block_size) ) {
     148    if ((returned == 0) && (bytes_read < block_size))
     149    {
    103150      /*DEBUG(0,("read_block: not a vald registry file ?\n" ));*/
    104151      return false;
    105152    }   
    106                
     153
    107154    bytes_read += returned;
    108155  }
     
    701748  /* get the data if necessary */
    702749
    703   if ( vk->data_size != 0 ) {
     750  if ( vk->data_size != 0 )
     751  {
    704752    bool charmode = false;
    705753
     
    708756
    709757    /* the data is stored in the offset if the size <= 4 */
    710 
    711     if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
     758    if ( !(vk->data_size & VK_DATA_IN_OFFSET) )
     759    {
    712760      REGF_HBIN *hblock = hbin;
    713761      uint32 data_rec_size;
    714762
    715       if ( hbin->ps.io ) {
     763      if ( hbin->ps.io )
     764      {
    716765        if ( !(vk->data = (uint8*)zcalloc(sizeof(uint8), vk->data_size) ) )
    717766          return false;
     
    719768
    720769      /* this data can be in another hbin */
    721       if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
     770      if ( !hbin_contains_offset( hbin, vk->data_off ) )
     771      {
    722772        if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
    723773          return false;
    724774      }
    725       if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
    726         return false;
    727 
    728       if ( !hblock->ps.io ) {
     775      if (!(prs_set_offset(&hblock->ps,
     776                           (vk->data_off
     777                            + HBIN_HDR_SIZE
     778                            - hblock->first_hbin_off)
     779                           - sizeof(uint32))))
     780      { return false; }
     781
     782      if ( !hblock->ps.io )
     783      {
    729784        data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
    730785        data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
     
    732787      if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
    733788        return false;
    734       if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
     789      if(!prs_uint8s(charmode, "data", &hblock->ps, depth,
     790                     vk->data, vk->data_size))
    735791        return false;
    736792
     
    738794        hblock->dirty = true;
    739795    }
    740     else {
    741       if ( !(vk->data = zcalloc(sizeof(uint8), 4) ) )
     796    else
     797    {
     798      if(!(vk->data = zcalloc(sizeof(uint8), 4)))
    742799        return false;
    743800      SIVAL( vk->data, 0, vk->data_off );
     
    749806
    750807  data_size = ((start_off - end_off ) & 0xfffffff8 );
    751   if ( data_size !=  vk->rec_size )
    752     /*DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));*/
    753 
    754     if ( !hbin->ps.io )
    755       hbin->dirty = true;
     808  /*if ( data_size !=  vk->rec_size )
     809    DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));*/
     810
     811  if ( !hbin->ps.io )
     812    hbin->dirty = true;
    756813
    757814  return true;
     
    763820 in the prs_struct *ps.
    764821*******************************************************************/
    765 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin,
    766                                  int depth, REGF_NK_REC *nk, REGF_FILE *file )
     822static bool hbin_prs_vk_records(const char *desc, REGF_HBIN *hbin,
     823                                int depth, REGF_NK_REC *nk, REGF_FILE *file)
    767824{
    768825  int i;
     
    772829       
    773830  /* check if we have anything to do first */
    774        
    775   if ( nk->num_values == 0 )
     831  if(nk->num_values == 0)
    776832    return true;
    777833               
    778   if ( hbin->ps.io ) {
    779     if ( !(nk->values = (REGF_VK_REC*)zcalloc(sizeof(REGF_VK_REC), nk->num_values ) ) )
     834  if(hbin->ps.io)
     835  {
     836    if (!(nk->values = (REGF_VK_REC*)zcalloc(sizeof(REGF_VK_REC),
     837                                              nk->num_values )))
    780838      return false;
    781839  }
    782840       
    783841  /* convert the offset to something relative to this HBIN block */
    784        
    785   if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
    786     return false;
    787 
    788   if ( !hbin->ps.io ) {
     842  if (!prs_set_offset(&hbin->ps,
     843                      nk->values_off
     844                      + HBIN_HDR_SIZE
     845                      - hbin->first_hbin_off
     846                      - sizeof(uint32)))
     847  { return false; }
     848
     849  if ( !hbin->ps.io )
     850  {
    789851    record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
    790852    record_size = (record_size - 1) ^ 0xFFFFFFFF;
     
    794856    return false;
    795857               
    796   for ( i=0; i<nk->num_values; i++ ) {
     858  for ( i=0; i<nk->num_values; i++ )
     859  {
    797860    if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
    798861      return false;
    799862  }
    800863
    801   for ( i=0; i<nk->num_values; i++ ) {
     864  for ( i=0; i<nk->num_values; i++ )
     865  {
    802866    REGF_HBIN *sub_hbin = hbin;
    803867    uint32 new_offset;
    804868       
    805     if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
     869    if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) )
     870    {
    806871      sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
    807       if ( !sub_hbin ) {
     872      if ( !sub_hbin )
     873      {
    808874        /*DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
    809875          nk->values[i].hbin_off));*/
     
    812878    }
    813879               
    814     new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
    815     if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
    816       return false;
    817     if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
     880    new_offset = nk->values[i].rec_off
     881      + HBIN_HDR_SIZE
     882      - sub_hbin->first_hbin_off;
     883
     884    if (!prs_set_offset(&sub_hbin->ps, new_offset))
     885      return false;
     886    if (!hbin_prs_vk_rec("vk_rec", sub_hbin, depth, &nk->values[i], file))
    818887      return false;
    819888  }
     
    822891    hbin->dirty = true;
    823892
    824 
    825893  return true;
    826894}
     
    829897/*******************************************************************
    830898 *******************************************************************/
    831 
    832899static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
    833900{
     
    842909}
    843910
    844 /*******************************************************************
    845  *******************************************************************/
    846 
     911
     912/*******************************************************************
     913 *******************************************************************/
    847914static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
    848915{
     
    859926}
    860927
    861 /*******************************************************************
    862  *******************************************************************/
    863 
     928
     929/*******************************************************************
     930 *******************************************************************/
    864931static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
    865932{
     
    876943  /* fill in values */
    877944       
    878   if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
     945  if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) )
     946  {
    879947    sub_hbin = hbin;
    880     if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
     948    if ( !hbin_contains_offset( hbin, nk->values_off ) )
     949    {
    881950      sub_hbin = lookup_hbin_block( file, nk->values_off );
    882       if ( !sub_hbin ) {
     951      if ( !sub_hbin )
     952      {
    883953        /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
    884954          nk->values_off));*/
     
    887957    }
    888958               
    889     if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
     959    if(!hbin_prs_vk_records("vk_rec", sub_hbin, depth, nk, file))
    890960      return false;
    891961  }
    892962               
    893963  /* now get subkeys */
    894        
    895   if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
     964  if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) )
     965  {
    896966    sub_hbin = hbin;
    897     if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
     967    if ( !hbin_contains_offset( hbin, nk->subkeys_off ) )
     968    {
    898969      sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
    899       if ( !sub_hbin ) {
     970      if ( !sub_hbin )
     971      {
    900972        /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
    901973          nk->subkeys_off));*/
     
    904976    }
    905977               
    906     if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
     978    if (!hbin_prs_lf_records("lf_rec", sub_hbin, depth, nk))
    907979      return false;
    908980  }
     
    910982  /* get the to the security descriptor.  First look if we have already parsed it */
    911983       
    912   if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
    913 
     984  if ((nk->sk_off!=REGF_OFFSET_NONE)
     985      && !(nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )))
     986  {
    914987    sub_hbin = hbin;
    915     if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
     988    if (!hbin_contains_offset(hbin, nk->sk_off))
     989    {
    916990      sub_hbin = lookup_hbin_block( file, nk->sk_off );
    917991      if ( !sub_hbin ) {
     
    9371011}
    9381012
    939 /*******************************************************************
    940  *******************************************************************/
    941 
     1013
     1014/*******************************************************************
     1015 *******************************************************************/
    9421016static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
    9431017{
     
    9541028  /* assume that the current offset is at the reacord header
    9551029     and we need to backup to read the record size */
    956 
    9571030  curr_off -= sizeof(uint32);
    9581031
    9591032  block_size = ps->buffer_size;
    9601033  record_size = 0;
    961   while ( !found ) {
    962 
     1034  while ( !found )
     1035  {
    9631036    curr_off = curr_off+record_size;
    9641037    if ( curr_off >= block_size )
     
    9851058
    9861059  /* mark prs_struct as done ( at end ) if no more SK records */
    987   /* mark end-of-block as true */
    988        
    989   if ( !found )
     1060  /* mark end-of-block as true */       
     1061  if ( !found )
    9901062  {
    9911063    prs_set_offset( &hbin->ps, hbin->ps.buffer_size );
     
    9931065    return false;
    9941066  }
    995                
    996   if ( !prs_set_offset( ps, curr_off ) )
     1067
     1068  if (!prs_set_offset(ps, curr_off))
    9971069    return false;
    9981070
     
    10031075/*******************************************************************
    10041076 *******************************************************************/
    1005 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
    1006 {
    1007   if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
     1077static bool next_nk_record(REGF_FILE *file, REGF_HBIN *hbin,
     1078                           REGF_NK_REC *nk, bool *eob)
     1079{
     1080  if (next_record(hbin, "nk", eob)
     1081      && hbin_prs_key(file, hbin, nk))
    10081082    return true;
    10091083       
     
    11611235
    11621236  /* see if there is anything left to report */
    1163        
    1164   if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
     1237  if (!nk || (nk->subkeys_off==REGF_OFFSET_NONE)
     1238      || (nk->subkey_index >= nk->num_subkeys))
    11651239    return NULL;
    11661240
    11671241  /* find the HBIN block which should contain the nk record */
    1168        
    11691242  if(!(hbin
    11701243       = lookup_hbin_block(file, nk->subkeys.hashes[nk->subkey_index].nk_off )))
     
    11761249       
    11771250  nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
    1178   if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
     1251  if(!prs_set_offset(&hbin->ps,
     1252                     (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off)))
    11791253    return NULL;
    11801254               
    11811255  nk->subkey_index++;
    1182   if (!(subkey = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC))))
    1183     return NULL;
    1184 
    1185   if ( !hbin_prs_key( file, hbin, subkey ) )
    1186     return NULL;
    1187        
     1256  if(!(subkey = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC))))
     1257    return NULL;
     1258
     1259  if(!hbin_prs_key(file, hbin, subkey))
     1260    return NULL;
     1261
    11881262  return subkey;
    11891263}
Note: See TracChangeset for help on using the changeset viewer.