X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?p=ossec-hids.git;a=blobdiff_plain;f=src%2Fshared%2Fstring_op.c;fp=src%2Fshared%2Fstring_op.c;h=d688d81512d6ad7a89c84c8b0ca74b00e10ca8ec;hp=f458793402006f3706f7c2c67e1c68b286fd8b5d;hb=789cbc8e52da68eba3517b920ef22e000cf3c9fd;hpb=ef70704f0b31b59bb719b884d6a99cb9e3e2044a 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 */