X-Git-Url: http://ftp.carnet.hr/pub/carnet-debian/scm?a=blobdiff_plain;f=src%2Fshared%2Fstring_op.c;h=d688d81512d6ad7a89c84c8b0ca74b00e10ca8ec;hb=e81e4e82e5115bf99b6fbd9ebd486de325d67ed6;hp=f458793402006f3706f7c2c67e1c68b286fd8b5d;hpb=6ef2f786c6c8ead94841b5f93baf9f43421f08c8;p=ossec-hids.git diff --git a/src/shared/string_op.c b/src/shared/string_op.c index f458793..d688d81 100755 --- a/src/shared/string_op.c +++ b/src/shared/string_op.c @@ -87,5 +87,45 @@ int os_substr(char *dest, const char *src, int position, int length) { return 0; } +/* Escape a set of characters */ +char *os_shell_escape(const char *src) { + // Maximum Length of the String is 2xthe current length + char shell_escapes[] = { '\\', '"', '\'', ' ', '\t', ';', '`', '>', '<', '|', '#', + '*', '[', ']', '{', '}', '&', '$', '!', ':', '(', ')' }; + + char *escaped_string; + int length = 0; + int i = 0; + + if (src == NULL) + return NULL; + + // Determine how long the string will be + const char *iterator = src; + for (; *iterator; iterator++) { + if( strchr(shell_escapes, *iterator) ) { + length++; + } + length++; + } + // Allocate the memory + if( (escaped_string = calloc(1, length + 1 )) == NULL ) { + // Return NULL + return NULL; + } + + // Escape the escapable characters + iterator=src; + for( i=0; *iterator; iterator++ ) { + if ( strchr(shell_escapes, *iterator) ) { + escaped_string[i] = '\\'; + i++; + } + escaped_string[i] = *iterator; + i++; + } + // Return Success + return escaped_string; +} /* EOF */