1 |
/* wptUtil.cpp - Helper functions |
/* wptUtil.cpp - Helper functions |
2 |
* Copyright (C) 2005 Timo Schulz |
* Copyright (C) 2005, 2006, 2008 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
15 |
*/ |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
18 |
#endif |
#endif |
19 |
|
|
20 |
|
#include <windows.h> |
21 |
#include <string.h> |
#include <string.h> |
22 |
#include <string.h> |
#include <stdlib.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
24 |
#include <ctype.h> |
#include <ctype.h> |
25 |
|
|
26 |
|
#include "wptTypes.h" |
27 |
|
#include "StringBuffer.h" |
28 |
|
|
29 |
/* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */ |
/* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */ |
30 |
extern "C" char* |
char* |
31 |
strsep (char **stringp, const char *delim) |
strsep (char **stringp, const char *delim) |
32 |
{ |
{ |
33 |
char *begin, *end; |
char *begin, *end; |
54 |
} |
} |
55 |
else /* Find the end of the token. */ |
else /* Find the end of the token. */ |
56 |
end = strpbrk (begin, delim); |
end = strpbrk (begin, delim); |
57 |
if ( end ){ |
if (end) { |
58 |
/* Terminate the token and set *STRINGP past NUL character. */ |
/* Terminate the token and set *STRINGP past NUL character. */ |
59 |
*end++ = '\0'; |
*end++ = '\0'; |
60 |
*stringp = end; |
*stringp = end; |
68 |
|
|
69 |
|
|
70 |
/* Like strstr but this version is case in-sentensive. */ |
/* Like strstr but this version is case in-sentensive. */ |
71 |
extern "C" const char * |
const char * |
72 |
stristr (const char *buf, const char *sub) |
stristr (const char *buf, const char *sub) |
73 |
{ |
{ |
74 |
const char *t, *s ; |
const char *t, *s ; |
75 |
size_t n; |
size_t n; |
76 |
size_t buflen = strlen (buf); |
size_t buflen = strlen (buf); |
77 |
|
|
78 |
for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) { |
for (t=buf, n=buflen, s=sub ; n ; t++, n--) { |
79 |
if( toupper(*t) == toupper(*s) ) { |
if (toupper (*t) == toupper (*s)) { |
80 |
for( buf=t++, buflen = n--, s++; |
for (buf=t++, buflen = n--, s++; |
81 |
n && toupper(*t) == toupper(*s); t++, s++, n-- ) |
n && toupper(*t) == toupper(*s); t++, s++, n--) |
82 |
; |
; |
83 |
if( !*s ) |
if (!*s) |
84 |
return buf; |
return buf; |
85 |
t = buf; n = buflen; s = sub ; |
t = buf; |
86 |
|
n = buflen; |
87 |
|
s = sub; |
88 |
} |
} |
89 |
} |
} |
90 |
return NULL ; |
return NULL ; |
91 |
} |
} |
92 |
|
|
93 |
|
|
94 |
|
/* Check if @fname is a valid file name on a Window file system. |
95 |
|
If is_path = 1, @fname is treated as a file name without any separators. */ |
96 |
|
int |
97 |
|
check_file_name (const char *fname, int is_path) |
98 |
|
{ |
99 |
|
const char *not_allowed = "/*?\"<>|"; |
100 |
|
|
101 |
|
/* If we are not in path mode (@is_path = 1) we also consider |
102 |
|
path separators as illegal chars. */ |
103 |
|
if (!is_path && (strchr (fname, ':') || strchr (fname, '\\'))) |
104 |
|
return -1; |
105 |
|
|
106 |
|
for (size_t i=0; i < strlen (fname); i++) { |
107 |
|
if (strchr (not_allowed, fname[i])) |
108 |
|
return -1; |
109 |
|
} |
110 |
|
return 0; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
/* Check if the email address @email only contain valid characters. |
115 |
|
Return 0 on success. */ |
116 |
|
int |
117 |
|
check_email_address (const char *email) |
118 |
|
{ |
119 |
|
const char *allowed = "@._-%+;"; |
120 |
|
size_t i, len = strlen (email); |
121 |
|
|
122 |
|
if (len < 3 || !strchr (email, '@')) |
123 |
|
return -1; |
124 |
|
for (i=0; i < len; i++) { |
125 |
|
if (isxdigit (email[i]) || isalpha (email[i]) |
126 |
|
|| strchr (allowed, email[i])) |
127 |
|
continue; |
128 |
|
else |
129 |
|
return -1; |
130 |
|
} |
131 |
|
return 0; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
/* Return a substring of @str from the position @begin |
136 |
|
to position @end. */ |
137 |
|
char* |
138 |
|
substr (const char *str, unsigned int begin, unsigned int end) |
139 |
|
{ |
140 |
|
char *p; |
141 |
|
size_t i, pos; |
142 |
|
|
143 |
|
if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0) |
144 |
|
return NULL; |
145 |
|
|
146 |
|
p = new char[end-begin+1]; |
147 |
|
if (!p) |
148 |
|
BUG (0); |
149 |
|
for (i = begin, pos=0; i < end; i++) |
150 |
|
p[pos++] = str[i]; |
151 |
|
p[pos] = '\0'; |
152 |
|
return p; |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
/* Remove %AB sequences from the input buffer @in |
157 |
|
and store the raw data in @out. */ |
158 |
|
void |
159 |
|
unhexify_buffer (const char *in, char **r_out) |
160 |
|
{ |
161 |
|
char temp[3], *out; |
162 |
|
size_t len, pos, i=0; |
163 |
|
|
164 |
|
len = strlen (in); |
165 |
|
out = new char[len+1]; |
166 |
|
if (!out) |
167 |
|
BUG (0); |
168 |
|
memset (out, 0, len+1); |
169 |
|
for (pos = 0; pos < len; pos++) { |
170 |
|
if (in[pos] == '%' && in[pos+1] == '%') |
171 |
|
out[i++] = '%'; |
172 |
|
else if (in[pos] == '%') { |
173 |
|
temp[0] = in[++pos]; |
174 |
|
temp[1] = in[++pos]; |
175 |
|
temp[2] = 0; |
176 |
|
out[i++] = (char)strtoul (temp, NULL, 16); |
177 |
|
} |
178 |
|
else |
179 |
|
out[i++] = in[pos]; |
180 |
|
} |
181 |
|
out[i] = 0; |
182 |
|
*r_out = out; |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
/* Safe strdup version (C++ version). */ |
187 |
|
char* |
188 |
|
m_strdup (const char *str) |
189 |
|
{ |
190 |
|
char *p = new char[strlen (str) + 1]; |
191 |
|
if (!p) |
192 |
|
BUG (0); |
193 |
|
strcpy (p, str); |
194 |
|
return p; |
195 |
|
} |