1 |
/* wptUtil.cpp - Helper functions |
2 |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
15 |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <string.h> |
22 |
#include <stdlib.h> |
23 |
#include <stdio.h> |
24 |
#include <ctype.h> |
25 |
|
26 |
#include "wptTypes.h" |
27 |
|
28 |
|
29 |
/* code taken from glibc-2.2.1/sysdeps/generic/strsep.c */ |
30 |
char* |
31 |
strsep (char **stringp, const char *delim) |
32 |
{ |
33 |
char *begin, *end; |
34 |
begin = *stringp; |
35 |
|
36 |
if (begin == NULL) |
37 |
return NULL; |
38 |
|
39 |
/* A frequent case is when the delimiter string contains only one |
40 |
character. Here we don't need to call the expensive `strpbrk' |
41 |
function and instead work using `strchr'. */ |
42 |
if (delim[0] == '\0' || delim[1] == '\0') { |
43 |
char ch = delim[0]; |
44 |
if (ch == '\0') |
45 |
end = NULL; |
46 |
else { |
47 |
if (*begin == ch) |
48 |
end = begin; |
49 |
else if (*begin == '\0') |
50 |
end = NULL; |
51 |
else |
52 |
end = strchr (begin + 1, ch); |
53 |
} |
54 |
} |
55 |
else /* Find the end of the token. */ |
56 |
end = strpbrk (begin, delim); |
57 |
if (end) { |
58 |
/* Terminate the token and set *STRINGP past NUL character. */ |
59 |
*end++ = '\0'; |
60 |
*stringp = end; |
61 |
} |
62 |
else |
63 |
/* No more delimiters; this is the last token. */ |
64 |
*stringp = NULL; |
65 |
|
66 |
return begin; |
67 |
} |
68 |
|
69 |
|
70 |
/* Like strstr but this version is case in-sentensive. */ |
71 |
const char * |
72 |
stristr (const char *buf, const char *sub) |
73 |
{ |
74 |
const char *t, *s ; |
75 |
size_t n; |
76 |
size_t buflen = strlen (buf); |
77 |
|
78 |
for (t=buf, n=buflen, s=sub ; n ; t++, n--) { |
79 |
if (toupper (*t) == toupper (*s)) { |
80 |
for( buf=t++, buflen = n--, s++; |
81 |
n && toupper(*t) == toupper(*s); t++, s++, n-- ) |
82 |
; |
83 |
if (!*s) |
84 |
return buf; |
85 |
t = buf; n = buflen; s = sub ; |
86 |
} |
87 |
} |
88 |
return NULL ; |
89 |
} |
90 |
|
91 |
|
92 |
/* Check if @fname is a valid file name on a Window file system. |
93 |
If is_path = 1, @fname is treated as a file name without any separators. */ |
94 |
int |
95 |
check_file_name (const char *fname, int is_path) |
96 |
{ |
97 |
const char *not_allowed = "/*?\"<>|"; |
98 |
size_t i; |
99 |
|
100 |
/* If we are not in path mode (@is_path = 1) we also consider |
101 |
path separators as illegal chars. */ |
102 |
if (!is_path && (strchr (fname, ':') || strchr (fname, '\\'))) |
103 |
return -1; |
104 |
|
105 |
for (i=0; i < strlen (fname); i++) { |
106 |
if (strchr (not_allowed, fname[i])) |
107 |
return -1; |
108 |
} |
109 |
return 0; |
110 |
} |
111 |
|
112 |
|
113 |
/* Check if the email address @email only contain valid characters. |
114 |
Return 0 on success. */ |
115 |
int |
116 |
check_email_address (const char *email) |
117 |
{ |
118 |
const char *allowed = "@._-%+;"; |
119 |
size_t i, len = strlen (email); |
120 |
|
121 |
if (len < 3 || !strchr (email, '@')) |
122 |
return -1; |
123 |
for (i=0; i < len; i++) { |
124 |
if (isxdigit (email[i]) || isalpha (email[i]) |
125 |
|| strchr (allowed, email[i])) |
126 |
continue; |
127 |
else |
128 |
return -1; |
129 |
} |
130 |
return 0; |
131 |
} |
132 |
|
133 |
|
134 |
/* Return a substring of @str from the position @begin |
135 |
to position @end. */ |
136 |
char* |
137 |
substr (const char *str, unsigned int begin, unsigned int end) |
138 |
{ |
139 |
char *p; |
140 |
size_t i, pos; |
141 |
|
142 |
if (end > strlen (str) || begin > strlen (str) || (end-begin) < 0) |
143 |
return NULL; |
144 |
|
145 |
p = new char[end-begin+1]; |
146 |
if (!p) |
147 |
BUG (0); |
148 |
for (i = begin, pos=0; i < end; i++) |
149 |
p[pos++] = str[i]; |
150 |
p[pos] = '\0'; |
151 |
return p; |
152 |
} |
153 |
|
154 |
|
155 |
/* Remove %AB sequences from the input buffer @in |
156 |
and store the raw data in @out. */ |
157 |
void |
158 |
unhexify_buffer (const char *in, char **r_out) |
159 |
{ |
160 |
char temp[3], *out; |
161 |
size_t len, pos, i=0; |
162 |
|
163 |
len = strlen (in); |
164 |
out = new char[len+1]; |
165 |
if (!out) |
166 |
abort (); |
167 |
memset (out, 0, len+1); |
168 |
for (pos = 0; pos < len; pos++) { |
169 |
if (in[pos] == '%' && in[pos+1] == '%') |
170 |
out[i++] = '%'; |
171 |
else if (in[pos] == '%') { |
172 |
temp[0] = in[++pos]; |
173 |
temp[1] = in[++pos]; |
174 |
temp[2] = 0; |
175 |
out[i++] = (char)strtoul (temp, NULL, 16); |
176 |
} |
177 |
else |
178 |
out[i++] = in[pos]; |
179 |
} |
180 |
out[i] = 0; |
181 |
*r_out = out; |
182 |
} |
183 |
|
184 |
|
185 |
/* Safe strdup version (C++ version). */ |
186 |
char* |
187 |
m_strdup (const char *str) |
188 |
{ |
189 |
char *p = new char[strlen (str) + 1]; |
190 |
if (!p) |
191 |
BUG (NULL); |
192 |
strcpy (p, str); |
193 |
return p; |
194 |
} |