Changeset 12 for trunk/src/sh_database.c


Ignore:
Timestamp:
Jan 5, 2006, 11:15:45 PM (19 years ago)
Author:
rainer
Message:

More optimisation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_database.c

    r11 r12  
    5353  int    val;
    5454  int    size;
     55  int    alen;
    5556  size_t off;
    5657} my_attr;
     
    124125} dbins;
    125126
     127static my_attr * attr_tab_srch = NULL;
     128static int       attr_tab_srch_siz = 0;
     129
    126130static my_attr attr_tab[] = {
    127   { NULL, N_("sev"),         0,   1,    8, offsetof(struct dbins_, sev) },
    128   { NULL, N_("tstamp"),      0,   2,   16, offsetof(struct dbins_, time) },
    129   { NULL, N_("remote_host"), 0,   3,   64, offsetof(struct dbins_, host) },
    130   { NULL, N_("msg"),         0,   4, 1024, offsetof(struct dbins_, msg) },
    131 
    132   { NULL, N_("path"),        0,   5,12288, offsetof(struct dbins_, path)  },
     131  { NULL, N_("sev"),         0,   1,    8, 0, offsetof(struct dbins_, sev) },
     132  { NULL, N_("tstamp"),      0,   2,   16, 0, offsetof(struct dbins_, time) },
     133  { NULL, N_("remote_host"), 0,   3,   64, 0, offsetof(struct dbins_, host) },
     134  { NULL, N_("msg"),         0,   4, 1024, 0, offsetof(struct dbins_, msg) },
     135
     136  { NULL, N_("path"),        0,   5,12288, 0, offsetof(struct dbins_, path)  },
    133137  /* username -> userid; replace (long) 'userid' - below - by 'dummy' */
    134   { NULL, N_("userid"),      0,   6,    9, offsetof(struct dbins_, user)  },
    135   { NULL, N_("group"),       0,   7,    9, offsetof(struct dbins_, group)  },
    136   { NULL, N_("program"),     0,   8,    8, offsetof(struct dbins_, program)  },
    137   { NULL, N_("subroutine"),  0,   9,   16, offsetof(struct dbins_, subroutine)},
    138   { NULL, N_("status"),      0,  10,   12, offsetof(struct dbins_, status)  },
    139   { NULL, N_("hash"),        0,  11,   50, offsetof(struct dbins_, hash)  },
    140   { NULL, N_("path_data"),   0,  12, 1024, offsetof(struct dbins_, path_data)  },
    141   { NULL, N_("hash_data"),   0,  13,   50, offsetof(struct dbins_, hash_data)  },
    142   { NULL, N_("key_uid"),     0,  14,   64, offsetof(struct dbins_, key_uid)  },
    143   { NULL, N_("key_uid_data"),0,  15,   64, offsetof(struct dbins_, key_uid_data)},
    144   { NULL, N_("key_id"),      0,  16,   16, offsetof(struct dbins_, key_id)  },
    145   { NULL, N_("module"),      0,  17,    8, offsetof(struct dbins_, module)  },
    146   { NULL, N_("syscall"),     0,  19,   16, offsetof(struct dbins_, syscall)  },
    147   { NULL, N_("ip"),          0,  20,   16, offsetof(struct dbins_, ip)  },
    148   { NULL, N_("tty"),         0,  21,   16, offsetof(struct dbins_, tty)  },
    149   { NULL, N_("peer"),        0,  22,   64, offsetof(struct dbins_, peer)  },
    150   { NULL, N_("obj"),         0,  23, 1024, offsetof(struct dbins_, obj)  },
    151   { NULL, N_("interface"),   0,  24,   64, offsetof(struct dbins_, interface)},
    152   { NULL, N_("time"),        0,  25,   64, offsetof(struct dbins_, ltime)  },
    153   { NULL, N_("dir"),         0,  26, 1024, offsetof(struct dbins_, dir)  },
    154   { NULL, N_("linked_path"), 0,  27, 1024, offsetof(struct dbins_, linked_path)},
    155   { NULL, N_("service"),     0,  29,   64, offsetof(struct dbins_, service)},
    156   { NULL, N_("facility"),    0,  30,   32, offsetof(struct dbins_, facility) },
    157   { NULL, N_("priority"),    0,  31,   32, offsetof(struct dbins_, priority) },
    158   { NULL, N_("syslog_msg"),  0,  32, 1024, offsetof(struct dbins_, syslog_msg)  },
    159 
    160   { NULL, N_("mode_old"),    0,  33,   16, offsetof(struct dbins_, mode_old) },
    161   { NULL, N_("mode_new"),    0,  34,   16, offsetof(struct dbins_, mode_new) },
    162   { NULL, N_("device_old"),  0,  35,   16, offsetof(struct dbins_, device_old)},
    163   { NULL, N_("device_new"),  0,  36,   16, offsetof(struct dbins_, device_new)},
    164   { NULL, N_("owner_old"),   0,  37,    9, offsetof(struct dbins_, owner_old)},
    165   { NULL, N_("owner_new"),   0,  38,    9, offsetof(struct dbins_, owner_new)},
    166   { NULL, N_("group_old"),   0,  39,    9, offsetof(struct dbins_, group_old)},
    167   { NULL, N_("group_new"),   0,  40,    9, offsetof(struct dbins_, group_new)},
    168   { NULL, N_("ctime_old"),   0,  41,   20, offsetof(struct dbins_, ctime_old)},
    169   { NULL, N_("ctime_new"),   0,  42,   20, offsetof(struct dbins_, ctime_new)},
    170   { NULL, N_("atime_old"),   0,  43,   20, offsetof(struct dbins_, atime_old)},
    171   { NULL, N_("atime_new"),   0,  44,   20, offsetof(struct dbins_, atime_new)},
    172   { NULL, N_("mtime_old"),   0,  45,   20, offsetof(struct dbins_, mtime_old)},
    173   { NULL, N_("mtime_new"),   0,  46,   20, offsetof(struct dbins_, mtime_new)},
    174   { NULL, N_("chksum_old"),  0,  47,   50, offsetof(struct dbins_, chksum_old)},
    175   { NULL, N_("chksum_new"),  0,  48,   50, offsetof(struct dbins_, chksum_new)},
    176   { NULL, N_("link_old"),    0,  49, 1024, offsetof(struct dbins_, link_old)},
    177   { NULL, N_("link_new"),    0,  50, 1024, offsetof(struct dbins_, link_new)},
    178                
    179   { NULL, N_("size_old"),     0,  51,    0, 0  },
    180   { NULL, N_("size_new"),     0,  52,    0, 0  },
    181   { NULL, N_("hardlinks_old"),0,  53,    0, 0  },
    182   { NULL, N_("hardlinks_new"),0,  54,    0, 0  },
    183   { NULL, N_("inode_old"),    0,  55,    0, 0  },
    184   { NULL, N_("inode_new"),    0,  56,    0, 0  },
    185 
    186   { NULL, N_("imode_old"),    0,  57,    0, 0  },
    187   { NULL, N_("imode_new"),    0,  58,    0, 0  },
    188   { NULL, N_("iattr_old"),    0,  59,    0, 0  },
    189   { NULL, N_("iattr_new"),    0,  60,    0, 0  },
    190   { NULL, N_("idevice_old"),  0,  61,    0, 0  },
    191   { NULL, N_("idevice_new"),  0,  62,    0, 0  },
    192   { NULL, N_("iowner_old"),   0,  63,    0, 0  },
    193   { NULL, N_("iowner_new"),   0,  64,    0, 0  },
    194   { NULL, N_("igroup_old"),   0,  65,    0, 0  },
    195   { NULL, N_("igroup_new"),   0,  66,    0, 0  },
    196 
    197   { NULL, N_("port"),         0,  67,    0, 0  },
    198   { NULL, N_("return_code"),  0,  68,    0, 0  },
     138  { NULL, N_("userid"),      0,   6,    9, 0, offsetof(struct dbins_, user)  },
     139  { NULL, N_("group"),       0,   7,    9, 0, offsetof(struct dbins_, group)  },
     140  { NULL, N_("program"),     0,   8,    8, 0, offsetof(struct dbins_, program)  },
     141  { NULL, N_("subroutine"),  0,   9,   16, 0, offsetof(struct dbins_, subroutine)},
     142  { NULL, N_("status"),      0,  10,   12, 0, offsetof(struct dbins_, status)  },
     143  { NULL, N_("hash"),        0,  11,   50, 0, offsetof(struct dbins_, hash)  },
     144  { NULL, N_("path_data"),   0,  12, 1024, 0, offsetof(struct dbins_, path_data)  },
     145  { NULL, N_("hash_data"),   0,  13,   50, 0, offsetof(struct dbins_, hash_data)  },
     146  { NULL, N_("key_uid"),     0,  14,   64, 0, offsetof(struct dbins_, key_uid)  },
     147  { NULL, N_("key_uid_data"),0,  15,   64, 0, offsetof(struct dbins_, key_uid_data)},
     148  { NULL, N_("key_id"),      0,  16,   16, 0, offsetof(struct dbins_, key_id)  },
     149  { NULL, N_("module"),      0,  17,    8, 0, offsetof(struct dbins_, module)  },
     150  { NULL, N_("syscall"),     0,  19,   16, 0, offsetof(struct dbins_, syscall)  },
     151  { NULL, N_("ip"),          0,  20,   16, 0, offsetof(struct dbins_, ip)  },
     152  { NULL, N_("tty"),         0,  21,   16, 0, offsetof(struct dbins_, tty)  },
     153  { NULL, N_("peer"),        0,  22,   64, 0, offsetof(struct dbins_, peer)  },
     154  { NULL, N_("obj"),         0,  23, 1024, 0, offsetof(struct dbins_, obj)  },
     155  { NULL, N_("interface"),   0,  24,   64, 0, offsetof(struct dbins_, interface)},
     156  { NULL, N_("time"),        0,  25,   64, 0, offsetof(struct dbins_, ltime)  },
     157  { NULL, N_("dir"),         0,  26, 1024, 0, offsetof(struct dbins_, dir)  },
     158  { NULL, N_("linked_path"), 0,  27, 1024, 0, offsetof(struct dbins_, linked_path)},
     159  { NULL, N_("service"),     0,  29,   64, 0, offsetof(struct dbins_, service)},
     160  { NULL, N_("facility"),    0,  30,   32, 0, offsetof(struct dbins_, facility) },
     161  { NULL, N_("priority"),    0,  31,   32, 0, offsetof(struct dbins_, priority) },
     162  { NULL, N_("syslog_msg"),  0,  32, 1024, 0, offsetof(struct dbins_, syslog_msg)  },
     163
     164  { NULL, N_("mode_old"),    0,  33,   16, 0, offsetof(struct dbins_, mode_old) },
     165  { NULL, N_("mode_new"),    0,  34,   16, 0, offsetof(struct dbins_, mode_new) },
     166  { NULL, N_("device_old"),  0,  35,   16, 0, offsetof(struct dbins_, device_old)},
     167  { NULL, N_("device_new"),  0,  36,   16, 0, offsetof(struct dbins_, device_new)},
     168  { NULL, N_("owner_old"),   0,  37,    9, 0, offsetof(struct dbins_, owner_old)},
     169  { NULL, N_("owner_new"),   0,  38,    9, 0, offsetof(struct dbins_, owner_new)},
     170  { NULL, N_("group_old"),   0,  39,    9, 0, offsetof(struct dbins_, group_old)},
     171  { NULL, N_("group_new"),   0,  40,    9, 0, offsetof(struct dbins_, group_new)},
     172  { NULL, N_("ctime_old"),   0,  41,   20, 0, offsetof(struct dbins_, ctime_old)},
     173  { NULL, N_("ctime_new"),   0,  42,   20, 0, offsetof(struct dbins_, ctime_new)},
     174  { NULL, N_("atime_old"),   0,  43,   20, 0, offsetof(struct dbins_, atime_old)},
     175  { NULL, N_("atime_new"),   0,  44,   20, 0, offsetof(struct dbins_, atime_new)},
     176  { NULL, N_("mtime_old"),   0,  45,   20, 0, offsetof(struct dbins_, mtime_old)},
     177  { NULL, N_("mtime_new"),   0,  46,   20, 0, offsetof(struct dbins_, mtime_new)},
     178  { NULL, N_("chksum_old"),  0,  47,   50, 0, offsetof(struct dbins_, chksum_old)},
     179  { NULL, N_("chksum_new"),  0,  48,   50, 0, offsetof(struct dbins_, chksum_new)},
     180  { NULL, N_("link_old"),    0,  49, 1024, 0, offsetof(struct dbins_, link_old)},
     181  { NULL, N_("link_new"),    0,  50, 1024, 0, offsetof(struct dbins_, link_new)},
     182                                            
     183  { NULL, N_("size_old"),     0,  51,    0, 0, 0  },
     184  { NULL, N_("size_new"),     0,  52,    0, 0, 0  },
     185  { NULL, N_("hardlinks_old"),0,  53,    0, 0, 0  },
     186  { NULL, N_("hardlinks_new"),0,  54,    0, 0, 0  },
     187  { NULL, N_("inode_old"),    0,  55,    0, 0, 0  },
     188  { NULL, N_("inode_new"),    0,  56,    0, 0, 0  },
     189                                           
     190  { NULL, N_("imode_old"),    0,  57,    0, 0, 0  },
     191  { NULL, N_("imode_new"),    0,  58,    0, 0, 0  },
     192  { NULL, N_("iattr_old"),    0,  59,    0, 0, 0  },
     193  { NULL, N_("iattr_new"),    0,  60,    0, 0, 0  },
     194  { NULL, N_("idevice_old"),  0,  61,    0, 0, 0  },
     195  { NULL, N_("idevice_new"),  0,  62,    0, 0, 0  },
     196  { NULL, N_("iowner_old"),   0,  63,    0, 0, 0  },
     197  { NULL, N_("iowner_new"),   0,  64,    0, 0, 0  },
     198  { NULL, N_("igroup_old"),   0,  65,    0, 0, 0  },
     199  { NULL, N_("igroup_new"),   0,  66,    0, 0, 0  },
     200
     201  { NULL, N_("port"),         0,  67,    0, 0, 0  },
     202  { NULL, N_("return_code"),  0,  68,    0, 0, 0  },
    199203  /* { NULL, N_("userid"),        0,  69,    0, 0  }, old 'userid', 1.8.1 */
    200204
    201   { NULL, N_("host"),         0,  70,   64, offsetof(struct dbins_, fromhost)},
    202   { NULL, N_("attr_old"),     0,  71,   16, offsetof(struct dbins_, attr_old)},
    203   { NULL, N_("attr_new"),     0,  72,   16, offsetof(struct dbins_, attr_new)},
    204 
    205   { NULL, NULL,      0,  0, 0, 0 }
     205  { NULL, N_("host"),         0,  70,   64, 0, offsetof(struct dbins_, fromhost)},
     206  { NULL, N_("attr_old"),     0,  71,   16, 0, offsetof(struct dbins_, attr_old)},
     207  { NULL, N_("attr_new"),     0,  72,   16, 0, offsetof(struct dbins_, attr_new)},
     208
     209  { NULL, NULL,      0,  0, 0, 0, 0 }
    206210};
    207211
     
    11791183        }
    11801184    }
     1185#if 0
     1186  /* apparently slower, see gyule.7 */
     1187  len = (long) strlen(val);
     1188
     1189  if ((val[0] != '\0') && (*size > 2))
     1190    {
     1191      if (flag == 1)
     1192        {
     1193          *end = ',';  ++end;
     1194          *end = '\''; ++end; (*size) -= 2;
     1195          *end = '\0';
     1196       
     1197          if ((long) *size > (len+2))
     1198            {
     1199              (void) sl_strlcat(end, val, (size_t) *size);
     1200              end   += len; (*size) -= len;
     1201              *end = '\''; ++end;  (*size) -= 1;
     1202            }
     1203          *end = '\0';
     1204        }
     1205      else
     1206        {
     1207          *end = ',';  ++end; (*size) -= 1;
     1208          *end = '\0';
     1209       
     1210          if ((long) *size > (len+1))
     1211            {
     1212              (void) sl_strlcat(end, val, (size_t) *size);
     1213              end   += len; (*size) -= len;
     1214            }
     1215          *end = '\0';
     1216        }
     1217    }
     1218#endif
    11811219  return end;
    11821220}
     
    13751413}
    13761414 
     1415static int sh_database_comp_attr (const void *m1, const void *m2)
     1416{
     1417  my_attr *mi1 = (my_attr *) m1;
     1418  my_attr *mi2 = (my_attr *) m2;
     1419  return strcmp(mi1->attr, mi2->attr);
     1420}
     1421
    13771422
    13781423static void init_attr_table()
    13791424{
    13801425  static  int first = S_TRUE;
    1381   int         i;
     1426  int         i, j;
    13821427
    13831428#ifdef SH_STEALTH
    13841429  int     j, k;
    13851430
    1386   if (first == S_TRUE)
    1387     {
    1388       i = 0;
    1389       while (attr_tab[i].attr_o != NULL)
    1390         {
    1391           j = strlen(attr_tab[i].attr_o);
    1392           attr_tab[i].attr = malloc (j+1); /* only once */
    1393           if (NULL == attr_tab[i].attr)
    1394             SL_RETURN (NULL, _("sh_database_parse"));
    1395           for (k = 0; k < j; ++k)
    1396             attr_tab[i].attr[k] = attr_tab[i].attr_o[k] ^ XOR_CODE;
    1397           attr_tab[i].attr[j] = '\0';
    1398           ++i;
    1399         }
    1400       first = S_FALSE;
    1401     }
     1431  if (first == S_FALSE)
     1432    return;
     1433
     1434  i = 0;
     1435  while (attr_tab[i].attr_o != NULL)
     1436    {
     1437      j = strlen(attr_tab[i].attr_o);
     1438      attr_tab[i].attr = malloc (j+1); /* only once */
     1439      if (NULL == attr_tab[i].attr)
     1440        SL_RETURN (NULL, _("sh_database_parse"));
     1441      for (k = 0; k < j; ++k)
     1442        attr_tab[i].attr[k] = attr_tab[i].attr_o[k] ^ XOR_CODE;
     1443      attr_tab[i].attr[j] = '\0';
     1444      attr_tab[i].alen = strlen(attr_tab[i].attr_o);
     1445      ++i;
     1446    }
     1447  first = S_FALSE;
     1448
    14021449#else
    1403   if (first == S_TRUE)
    1404     {
    1405       i = 0;
    1406       while (attr_tab[i].attr_o != NULL)
    1407         {
    1408           attr_tab[i].attr = attr_tab[i].attr_o;
    1409           ++i;
    1410         }
    1411       first = S_FALSE;
    1412     }
     1450
     1451  i = 0;
     1452  while (attr_tab[i].attr_o != NULL)
     1453    {
     1454      attr_tab[i].attr = attr_tab[i].attr_o;
     1455      attr_tab[i].alen = strlen(attr_tab[i].attr_o);
     1456      ++i;
     1457    }
     1458  first = S_FALSE;
     1459
    14131460#endif
     1461
     1462  /* create a sorted table for binary search
     1463   */
     1464  attr_tab_srch = SH_ALLOC(i * sizeof(my_attr));
     1465  for (j=0; j<i; ++j)
     1466    memcpy(&attr_tab_srch[j], &attr_tab[j], sizeof(my_attr));
     1467  qsort(attr_tab_srch, i, sizeof(my_attr), sh_database_comp_attr);
     1468  attr_tab_srch_siz = i;
     1469
    14141470  return;
    14151471}
     
    14791535  int     i;
    14801536  size_t  j;
     1537  my_attr * res;
     1538  my_attr key;
     1539  char    key_str[64];
    14811540
    14821541  SL_ENTER(_("sh_database_parse"));
     
    14971556  while ((p != NULL) && (*p != '\0') && (*p != '>'))
    14981557    {
    1499       if (p[0] == '/' && p[1] == '>')
     1558      if (p[0] == 'l' && p[1] == 'o' && p[2] == 'g' &&
     1559          (p[3] == ' ' || p[3] == '>'))
     1560        {
     1561          p = &p[4];
     1562          goto parse;
     1563        }
     1564      else if (p[0] == '/' && p[1] == '>')
    15001565        SL_RETURN (&p[2], _("sh_database_parse"));
    1501       if (p[0] == '/' && p[1] == 'l' && p[2] == 'o' &&
     1566      else if (p[0] == '/' && p[1] == 'l' && p[2] == 'o' &&
    15021567          p[3] == 'g' && p[4] == '>')
    15031568        SL_RETURN (&p[5], _("sh_database_parse"));
    1504       if (p[0] == 'l' && p[1] == 'o' && p[2] == 'g' &&
    1505           (p[3] == ' ' || p[3] == '>'))
    1506         {
    1507           p = &p[4];
    1508           goto parse;
    1509         }
    15101569      ++p;
    15111570    }
     
    15191578  if (!p || *p == '\0')
    15201579    SL_RETURN(NULL, _("sh_database_parse"));
     1580
     1581  if (*p != '<' && *p != '/')
     1582    goto par2;
    15211583
    15221584  if (p[0] == '<' && p[1] == 'l' &&
     
    15361598    SL_RETURN (&p[5], _("sh_database_parse"));
    15371599
     1600 par2:
    15381601
    15391602  /* non-whitespace
    15401603   */
    15411604  i = 0;
    1542   while (attr_tab[i].attr != NULL)
    1543     {
    1544       j = strlen(attr_tab[i].attr);
    1545       if (0 == strncmp(p, attr_tab[i].attr, j) &&
    1546           p[j] == '=' && p[j+1] == '"')
     1605  for (i=0; i < 64; ++i)
     1606    {
     1607      key_str[i] = p[i];
     1608      if (p[i] == '=')
     1609        {
     1610          key_str[i] = '\0';
     1611          break;
     1612        }
     1613    }
     1614  key_str[63] = '\0';
     1615  key.attr = &key_str[0];
     1616
     1617  res = bsearch(&key, attr_tab_srch, attr_tab_srch_siz,
     1618                sizeof(my_attr), sh_database_comp_attr);
     1619
     1620  if (res != NULL)
     1621    {
     1622      j = res->alen; /* strlen(attr_tab[i].attr); */
     1623      if (p[j] == '=' && p[j+1] == '"')
    15471624        {
    15481625          q = strchr(&p[j+2], '"');
     
    15621639              }
    15631640
    1564               if      (attr_tab[i].val == 1)
     1641              if      (res->val == 1)
    15651642                (void) sl_strlcpy(db_entry->sev, &p[j+2],
    1566                                   (size_t)attr_tab[i].size);
    1567               else if (attr_tab[i].val == 2)
     1643                                  (size_t)res->size);
     1644              else if (res->val == 2)
    15681645                {
    15691646                  z = strchr(&p[j+2], 'T');
     
    15711648                  (void) sl_strlcpy(db_entry->time, &p[j+2],  20);
    15721649                }
    1573               else if (attr_tab[i].val == 3)
     1650              else if (res->val == 3)
    15741651                (void) sl_strlcpy(db_entry->host, &p[j+2],
    1575                                   (size_t) attr_tab[i].size);
    1576               else if (attr_tab[i].val == 4)
     1652                                  (size_t) res->size);
     1653              else if (res->val == 4)
    15771654                (void) sl_strlcpy(db_entry->msg,  &p[j+2],
    1578                                   (size_t) attr_tab[i].size);
    1579               else if (attr_tab[i].size != 0)
     1655                                  (size_t) res->size);
     1656              else if (res->size != 0)
    15801657                {
    1581                   (void) sl_strlcpy( (((char *)(db_entry))+ attr_tab[i].off),
     1658                  (void) sl_strlcpy( (((char *)(db_entry))+ res->off),
    15821659                                     &p[j+2],
    1583                                      (size_t) attr_tab[i].size);
     1660                                     (size_t) res->size);
    15841661                }
    1585               else if (attr_tab[i].val >= START_SEC_LONGS)
     1662              else if (res->val >= START_SEC_LONGS)
    15861663                {
    1587                   db_entry->long_data[attr_tab[i].val-START_SEC_LONGS]
     1664                  db_entry->long_data[res->val-START_SEC_LONGS]
    15881665                    = atol(&p[j+2]);
    15891666                }
Note: See TracChangeset for help on using the changeset viewer.