]> granicus.if.org Git - p11-kit/commitdiff
path: Add p11_path_canon() function
authorStef Walter <stef@thewalter.net>
Wed, 3 Jul 2013 08:38:19 +0000 (10:38 +0200)
committerStef Walter <stef@thewalter.net>
Wed, 3 Jul 2013 09:49:26 +0000 (11:49 +0200)
Cleans up a filename with readable characters.

common/path.c
common/path.h
common/tests/test-path.c
trust/extract-info.c

index 8362765ac397949aa0b4f7e4462fdf84e9c97840..89d9a673fe769fb5484388822417df27791f2d06 100644 (file)
@@ -315,3 +315,18 @@ p11_path_prefix (const char *string,
               strncmp (string, prefix, b) == 0 &&
               is_path_component_or_null (string[b]);
 }
+
+void
+p11_path_canon (char *name)
+{
+       static const char *VALID =
+               "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_";
+       int i;
+
+       return_if_fail (name != NULL);
+
+       for (i = 0; name[i] != '\0'; i++) {
+               if (strchr (VALID, name[i]) == NULL)
+                       name[i] = '_';
+       }
+}
index cd135cb99ebce3062bf8b75fce57ece99a364a97..0b19a5d71998d72038ebbb008e62c0f8c96ddce9 100644 (file)
@@ -64,4 +64,6 @@ char *       p11_path_parent    (const char *path);
 bool         p11_path_prefix    (const char *string,
                                  const char *prefix);
 
+void         p11_path_canon     (char *name);
+
 #endif /* P11_PATH_H__ */
index 1671381fbfe38a110899841684ebc78b722c2b9c..1f85dbbfa4a70b5fdd59db4600b33e55b3252ea9 100644 (file)
@@ -201,6 +201,22 @@ test_prefix (void)
        assert (p11_path_prefix ("/test//other//second", "/test"));
 }
 
+static void
+test_canon (void)
+{
+       char *test;
+
+       test = strdup ("2309haonutb;AOE@#$O ");
+       p11_path_canon (test);
+       assert_str_eq (test, "2309haonutb_AOE___O_");
+       free (test);
+
+       test = strdup ("22@# %ATI@#$onot");
+       p11_path_canon (test);
+       assert_str_eq (test, "22____ATI___onot");
+       free (test);
+}
+
 int
 main (int argc,
       char *argv[])
@@ -211,6 +227,7 @@ main (int argc,
        p11_test (test_absolute, "/path/absolute");
        p11_test (test_parent, "/path/parent");
        p11_test (test_prefix, "/path/prefix");
+       p11_test (test_canon, "/path/canon");
 
        return p11_test_run (argc, argv);
 }
index ec25bc16d53fef636c4b2e1334c8011e124a8b79..f125b8a8034ac5c1cadf1eb49edbdcde1614ee1c 100644 (file)
@@ -42,6 +42,7 @@
 #include "dict.h"
 #include "extract.h"
 #include "message.h"
+#include "path.h"
 #include "pkcs11.h"
 #include "pkcs11x.h"
 #include "x509.h"
@@ -442,23 +443,15 @@ extract_label (p11_extract_info *extract)
        return strdup ("unknown");
 }
 
-#define FILENAME_CHARS \
-       "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_"
-
 char *
 p11_extract_info_filename (p11_extract_info *extract)
 {
        char *label;
-       int i;
 
        label = extract_label (extract);
        return_val_if_fail (label != NULL, NULL);
 
-       for (i = 0; label[i] != '\0'; i++) {
-               if (strchr (FILENAME_CHARS, label[i]) == NULL)
-                       label[i] = '_';
-       }
-
+       p11_path_canon (label);
        return label;
 }