Hash calculation.
authorJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Fri, 3 Dec 2010 07:38:26 +0000 (23:38 -0800)
committerJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Fri, 3 Dec 2010 07:38:26 +0000 (23:38 -0800)
src/smack_labels.c
src/smack_rules.c
tests/check_rules.c
tests/data/rule_set_read_from_file_and_save_to_kernel-excepted.txt

index fd7607b..ee3e7f3 100644 (file)
@@ -125,17 +125,24 @@ int smack_label_set_save_to_file(SmackLabelSet handle, const char *path)
 
 const char *smack_label_set_add(SmackLabelSet handle, const char *long_name)
 {
-       char sl[SMACK64_LEN + 1];
-       int pos, len ,ret;
+       char short_name[SMACK64_LEN + 1];
+       int pos, len;
        struct smack_label *l;
+       uint32_t h;
+       int i, c;
 
-       len = strlen(long_name);
-       pos = (len > SMACK64_LEN) ? len - SMACK64_LEN : 0;
+       // djb2 based on http://www.cse.yorku.ca/~oz/hash.html
+       h = 5381;
 
-       strcpy(sl, &long_name[pos]);
+       for (i = 0; long_name[i] != '\0'; i++) {
+               c = long_name[i];
+               h = ((h << 5) + h) + c;
+       }
+
+       sprintf(short_name, "%04X", h);
 
        l  = add_label(&handle->label_by_long_name, &handle->label_by_short_name,
-                      long_name, sl);
+                      long_name, short_name);
 
        return l != NULL ? l->short_name : NULL;
 }
index a36b198..61454a3 100644 (file)
@@ -104,6 +104,11 @@ SmackRuleSet smack_rule_set_new_from_file(const char *path,
                        ostr = object;
                }
 
+               if (sstr == NULL || ostr == NULL) {
+                       ret = -1;
+                       break;
+               }
+
                if (subject_filter == NULL ||
                         strcmp(sstr, subject_filter) == 0) {
                        ac = str_to_ac(access);
index 39c0d2a..115f6e3 100644 (file)
@@ -50,6 +50,9 @@ START_TEST(test_rule_set_read_from_file_and_save_to_kernel)
                "data/rule_set_read_from_file_and_save_to_kernel-in.txt", NULL, labels);
        fail_unless(rules != NULL, "Reading rules failed");
 
+       if (rules == NULL)
+               return;
+
        rc = smack_rule_set_save_to_file(rules,
                "rule_set_read_from_file_and_save_to_kernel-result.txt",
                NULL);
index 8ed792d..1c7c6c1 100644 (file)
@@ -1,2 +1,2 @@
-ooFooFooFooFooFooFooFoo arBarBarBarBarBarBarBar rx
-arBarBarBarBarBarBarBar ooFooFooFooFooFooFooFoo rwa
+E6BE6519 E1CEA9D6 rx
+E1CEA9D6 E6BE6519 rwa