new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / string_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "string.h"
12
13
14 /* Trim CR and/or LF from the last positions of a string */
15 void os_trimcrlf(char *str)
16 {
17     size_t len;
18
19     len = strlen(str);
20     len--;
21
22     while (str[len] == '\n' || str[len] == '\r') {
23         str[len] = '\0';
24         len--;
25     }
26 }
27
28 /* Remove offending char (e.g., double quotes) from source */
29 char *os_strip_char(const char *source, char remove)
30 {
31     char *clean;
32     const char *iterator = source;
33     size_t length = 0;
34     int i;
35
36     /* Figure out how much memory to allocate */
37     for ( ; *iterator; iterator++ ) {
38         if ( *iterator != remove ) {
39             length++;
40         }
41     }
42
43     /* Allocate the memory */
44     if ( (clean = (char *) malloc( length + 1 )) == NULL ) {
45         // Return NULL
46         return NULL;
47     }
48     memset(clean, '\0', length + 1);
49
50     /* Remove the characters */
51     iterator = source;
52     for ( i = 0; *iterator; iterator++ ) {
53         if ( *iterator != remove ) {
54             clean[i] = *iterator;
55             i++;
56         }
57     }
58
59     return clean;
60 }
61
62 /* Do a substring */
63 int os_substr(char *dest, const char *src, size_t position, ssize_t length)
64 {
65     dest[0] = '\0';
66
67     if ( length <= 0  ) {
68         /* Unsupported negative length string */
69         return -3;
70     }
71     if ( src == NULL ) {
72         return -2;
73     }
74     if ( position >= strlen(src) ) {
75         return -1;
76     }
77
78     strncat(dest, (src + position), (size_t) length);
79
80     return 0;
81 }
82
83 /* Escape a set of characters */
84 char *os_shell_escape(const char *src)
85 {
86     /* Maximum Length of the String is 2 times the current length */
87     char shell_escapes[] = { '\\', '"', '\'', ' ', '\t', ';', '`', '>', '<', '|', '#',
88                              '*', '[', ']', '{', '}', '&', '$', '!', ':', '(', ')'
89                            };
90
91     char *escaped_string;
92     size_t length = 0;
93     int i = 0;
94
95     if (src == NULL) {
96         return NULL;
97     }
98
99     /* Determine how long the string will be */
100     const char *iterator = src;
101     for (; *iterator; iterator++) {
102         if ( strchr(shell_escapes, *iterator) ) {
103             length++;
104         }
105         length++;
106     }
107     /* Allocate memory */
108     if ( (escaped_string = (char *) calloc(1, length + 1 )) == NULL ) {
109         // Return NULL
110         return NULL;
111     }
112
113     /* Escape the escapable characters */
114     iterator = src;
115     for ( i = 0; *iterator; iterator++ ) {
116         if ( strchr(shell_escapes, *iterator) ) {
117             escaped_string[i] = '\\';
118             i++;
119         }
120         escaped_string[i] = *iterator;
121         i++;
122     }
123
124     return escaped_string;
125 }
126