X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?a=blobdiff_plain;f=src%2Fshared%2Fstring_op.c;h=9bb67fad65891264044931fcc27ae7e4722d78eb;hb=3f728675941dc69d4e544d3a880a56240a6e394a;hp=d688d81512d6ad7a89c84c8b0ca74b00e10ca8ec;hpb=789cbc8e52da68eba3517b920ef22e000cf3c9fd;p=ossec-hids.git diff --git a/src/shared/string_op.c b/src/shared/string_op.c old mode 100755 new mode 100644 index d688d81..9bb67fa --- a/src/shared/string_op.c +++ b/src/shared/string_op.c @@ -1,6 +1,3 @@ -/* @(#) $Id: ./src/shared/string_op.c, 2011/11/01 dcid Exp $ - */ - /* Copyright (C) 2009 Trend Micro Inc. * All rights reserved. * @@ -8,56 +5,51 @@ * and/or modify it under the terms of the GNU General Public * License (version 2) as published by the FSF - Free Software * Foundation - * - * License details at the LICENSE file included with OSSEC or - * online at: http://www.ossec.net/en/licensing.html */ - #include "shared.h" #include "string.h" -/** os_trimcrlf - * Trims the cr and/or LF from the last positions of a string - */ + +/* Trim CR and/or LF from the last positions of a string */ void os_trimcrlf(char *str) { - int len; + size_t len; - len=strlen(str); + len = strlen(str); len--; - while (str[len]=='\n' || str[len]=='\r') - { - str[len]='\0'; - len--; + while (str[len] == '\n' || str[len] == '\r') { + str[len] = '\0'; + len--; } } /* Remove offending char (e.g., double quotes) from source */ -char *os_strip_char(char *source, char remove) { +char *os_strip_char(const char *source, char remove) +{ char *clean; - char *iterator = source; - int length = 0; + const char *iterator = source; + size_t length = 0; int i; - // Figure out how much memory to allocate - for( ; *iterator; iterator++ ) { + /* Figure out how much memory to allocate */ + for ( ; *iterator; iterator++ ) { if ( *iterator != remove ) { length++; } } - // Allocate the memory - if( (clean = malloc( length + 1 )) == NULL ) { + /* Allocate the memory */ + if ( (clean = (char *) malloc( length + 1 )) == NULL ) { // Return NULL return NULL; } memset(clean, '\0', length + 1); - // Remove the characters - iterator=source; - for( i=0; *iterator; iterator++ ) { + /* Remove the characters */ + iterator = source; + for ( i = 0; *iterator; iterator++ ) { if ( *iterator != remove ) { clean[i] = *iterator; i++; @@ -68,55 +60,59 @@ char *os_strip_char(char *source, char remove) { } /* Do a substring */ -int os_substr(char *dest, const char *src, int position, int length) { - dest[0]='\0'; +int os_substr(char *dest, const char *src, size_t position, ssize_t length) +{ + dest[0] = '\0'; - if( length <= 0 ) { - // Unsupported negative length string + if ( length <= 0 ) { + /* Unsupported negative length string */ return -3; } - if( src == NULL ) { + if ( src == NULL ) { return -2; } - if( position >= strlen(src) ) { + if ( position >= strlen(src) ) { return -1; } - strncat(dest, (src + position), length); - // Return Success + strncat(dest, (src + position), (size_t) 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 *os_shell_escape(const char *src) +{ + /* Maximum Length of the String is 2 times the current length */ char shell_escapes[] = { '\\', '"', '\'', ' ', '\t', ';', '`', '>', '<', '|', '#', - '*', '[', ']', '{', '}', '&', '$', '!', ':', '(', ')' }; + '*', '[', ']', '{', '}', '&', '$', '!', ':', '(', ')' + }; char *escaped_string; - int length = 0; + size_t length = 0; int i = 0; - if (src == NULL) + if (src == NULL) { return NULL; + } - // Determine how long the string will be + /* Determine how long the string will be */ const char *iterator = src; for (; *iterator; iterator++) { - if( strchr(shell_escapes, *iterator) ) { + if ( strchr(shell_escapes, *iterator) ) { length++; } length++; } - // Allocate the memory - if( (escaped_string = calloc(1, length + 1 )) == NULL ) { + /* Allocate memory */ + if ( (escaped_string = (char *) calloc(1, length + 1 )) == NULL ) { // Return NULL return NULL; } - // Escape the escapable characters - iterator=src; - for( i=0; *iterator; iterator++ ) { + /* Escape the escapable characters */ + iterator = src; + for ( i = 0; *iterator; iterator++ ) { if ( strchr(shell_escapes, *iterator) ) { escaped_string[i] = '\\'; i++; @@ -124,8 +120,7 @@ char *os_shell_escape(const char *src) { escaped_string[i] = *iterator; i++; } - // Return Success + return escaped_string; } -/* EOF */