]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
Adding a helper function that translates single characters in a string.
authorTobias Brunner <tobias@strongswan.org>
Mon, 8 Mar 2010 14:26:09 +0000 (15:26 +0100)
committerTobias Brunner <tobias@strongswan.org>
Mon, 8 Mar 2010 14:34:38 +0000 (15:34 +0100)
src/libstrongswan/plugins/plugin_loader.c
src/libstrongswan/utils.c
src/libstrongswan/utils.h

index 5dfeb873fad3da7c31eb1165a54ea50f2079d466..abcb84440f98faa441105afa02cf068d81ce29b4 100644 (file)
@@ -50,23 +50,6 @@ struct private_plugin_loader_t {
        linked_list_t *names;
 };
 
-/**
- * Replace '-' with '_' to use str as identifier.
- */
-static char* sanitize(char *str)
-{
-       char *pos = str;
-       while (pos && *pos)
-       {
-               if (*pos == '-')
-               {
-                       *pos = '_';
-               }
-               pos++;
-       }
-       return str;
-}
-
 #ifdef MONOLITHIC
 /**
  * load a single plugin in monolithic mode
@@ -83,7 +66,7 @@ static plugin_t* load_plugin(private_plugin_loader_t *this,
        {
                return NULL;
        }
-       sanitize(create);
+       translate(create, "-", "_");
        constructor = dlsym(RTLD_DEFAULT, create);
        if (constructor == NULL)
        {
@@ -120,7 +103,7 @@ static plugin_t* load_plugin(private_plugin_loader_t *this,
        {
                return NULL;
        }
-       sanitize(create);
+       translate(create, "-", "_");
        if (lib->integrity)
        {
                if (!lib->integrity->check_file(lib->integrity, name, file))
index b1b860351fe699f0aba99e955a53f12bf3013d5f..fd313843ee4605576aaf5953f8f66691b34e38e2 100644 (file)
@@ -116,6 +116,28 @@ void *memstr(const void *haystack, const char *needle, size_t n)
        return NULL;
 }
 
+/**
+ * Described in header.
+ */
+char* translate(char *str, const char *from, const char *to)
+{
+       char *pos = str;
+       if (strlen(from) != strlen(to))
+       {
+               return str;
+       }
+       while (pos && *pos)
+       {
+               char *match;
+               if ((match = strchr(from, *pos)) != NULL)
+               {
+                       *pos = to[match - from];
+               }
+               pos++;
+       }
+       return str;
+}
+
 /**
  * Described in header.
  */
index d3500258fb0eba8661f9a5200b5524a7359466e4..607d077f7bab48d85d4403495e62f52605091a32 100644 (file)
@@ -310,6 +310,14 @@ void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
  */
 void *memstr(const void *haystack, const char *needle, size_t n);
 
+/**
+ * Translates the characters in the given string, searching for characters
+ * in 'from' and mapping them to characters in 'to'.
+ * The two characters sets 'from' and 'to' must contain the same number of
+ * characters.
+ */
+char *translate(char *str, const char *from, const char *to);
+
 /**
  * Creates a directory and all required parent directories.
  *