/src/FreeRDP/winpr/libwinpr/crt/string.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * WinPR: Windows Portable Runtime  | 
3  |  |  * String Manipulation (CRT)  | 
4  |  |  *  | 
5  |  |  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
6  |  |  *  | 
7  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
8  |  |  * you may not use this file except in compliance with the License.  | 
9  |  |  * You may obtain a copy of the License at  | 
10  |  |  *  | 
11  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
12  |  |  *  | 
13  |  |  * Unless required by applicable law or agreed to in writing, software  | 
14  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
15  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
16  |  |  * See the License for the specific language governing permissions and  | 
17  |  |  * limitations under the License.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #include <winpr/config.h>  | 
21  |  | #include <winpr/assert.h>  | 
22  |  |  | 
23  |  | #include <errno.h>  | 
24  |  | #include <stdio.h>  | 
25  |  | #include <ctype.h>  | 
26  |  | #include <wctype.h>  | 
27  |  | #include <wchar.h>  | 
28  |  |  | 
29  |  | #include <winpr/crt.h>  | 
30  |  | #include <winpr/endian.h>  | 
31  |  |  | 
32  |  | #if defined(WITH_URIPARSER)  | 
33  |  | #include <uriparser/Uri.h>  | 
34  |  | #endif  | 
35  |  |  | 
36  |  | /* String Manipulation (CRT): http://msdn.microsoft.com/en-us/library/f0151s4x.aspx */  | 
37  |  |  | 
38  |  | #include "../log.h"  | 
39  |  | #define TAG WINPR_TAG("crt") | 
40  |  |  | 
41  |  | #ifndef MIN  | 
42  |  | #define MIN(x, y) (((x) < (y)) ? (x) : (y))  | 
43  |  | #endif  | 
44  |  |  | 
45  |  | #if defined(WITH_URIPARSER)  | 
46  |  | char* winpr_str_url_decode(const char* str, size_t len)  | 
47  |  | { | 
48  |  |   char* dst = strndup(str, len);  | 
49  |  |   if (!dst)  | 
50  |  |     return NULL;  | 
51  |  |  | 
52  |  |   if (!uriUnescapeInPlaceExA(dst, URI_FALSE, URI_FALSE))  | 
53  |  |   { | 
54  |  |     free(dst);  | 
55  |  |     return NULL;  | 
56  |  |   }  | 
57  |  |  | 
58  |  |   return dst;  | 
59  |  | }  | 
60  |  |  | 
61  |  | char* winpr_str_url_encode(const char* str, size_t len)  | 
62  |  | { | 
63  |  |   char* dst = calloc(len + 1, sizeof(char) * 3);  | 
64  |  |   if (!dst)  | 
65  |  |     return NULL;  | 
66  |  |  | 
67  |  |   if (!uriEscapeA(str, dst, URI_FALSE, URI_FALSE))  | 
68  |  |   { | 
69  |  |     free(dst);  | 
70  |  |     return NULL;  | 
71  |  |   }  | 
72  |  |   return dst;  | 
73  |  | }  | 
74  |  |  | 
75  |  | #else  | 
76  |  | static const char rfc3986[] = { | 
77  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
78  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
79  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x2e, 0x00,  | 
80  |  |   0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
81  |  |   0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,  | 
82  |  |   0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x5f,  | 
83  |  |   0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,  | 
84  |  |   0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x7e, 0x00,  | 
85  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
86  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
87  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
88  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
89  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
90  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
91  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
92  |  |   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  | 
93  |  | };  | 
94  |  |  | 
95  |  | static char hex2bin(char what)  | 
96  | 0  | { | 
97  | 0  |   if (what >= 'a')  | 
98  | 0  |     what -= 'a' - 'A';  | 
99  | 0  |   if (what >= 'A')  | 
100  | 0  |     what -= ('A' - 10); | 
101  | 0  |   else  | 
102  | 0  |     what -= '0';  | 
103  | 0  |   return what;  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | static char unescape(const char* what, size_t* px)  | 
107  | 0  | { | 
108  | 0  |   if ((*what == '%') && (isxdigit(what[1]) && isxdigit(what[2])))  | 
109  | 0  |   { | 
110  | 0  |     *px += 2;  | 
111  | 0  |     return 16 * hex2bin(what[1]) + hex2bin(what[2]);  | 
112  | 0  |   }  | 
113  |  |  | 
114  | 0  |   return *what;  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | char* winpr_str_url_decode(const char* str, size_t len)  | 
118  | 0  | { | 
119  | 0  |   char* dst = calloc(len + 1, sizeof(char));  | 
120  | 0  |   if (!dst)  | 
121  | 0  |     return NULL;  | 
122  |  |  | 
123  | 0  |   size_t pos = 0;  | 
124  | 0  |   for (size_t x = 0; x < strnlen(str, len); x++)  | 
125  | 0  |   { | 
126  | 0  |     const char* cur = &str[x];  | 
127  | 0  |     dst[pos++] = unescape(cur, &x);  | 
128  | 0  |   }  | 
129  | 0  |   return dst;  | 
130  | 0  | }  | 
131  |  |  | 
132  |  | static char* escape(char* dst, char what)  | 
133  | 0  | { | 
134  | 0  |   if (rfc3986[what & 0xff])  | 
135  | 0  |   { | 
136  | 0  |     *dst = what;  | 
137  | 0  |     return dst + 1;  | 
138  | 0  |   }  | 
139  |  |  | 
140  | 0  |   sprintf(dst, "%%%02" PRIX8, (BYTE)(what & 0xff));  | 
141  | 0  |   return dst + 3;  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | char* winpr_str_url_encode(const char* str, size_t len)  | 
145  | 0  | { | 
146  | 0  |   char* dst = calloc(len + 1, sizeof(char) * 3);  | 
147  | 0  |   if (!dst)  | 
148  | 0  |     return NULL;  | 
149  |  |  | 
150  | 0  |   char* ptr = dst;  | 
151  | 0  |   for (size_t x = 0; x < strnlen(str, len); x++)  | 
152  | 0  |   { | 
153  | 0  |     const char cur = str[x];  | 
154  | 0  |     ptr = escape(ptr, cur);  | 
155  | 0  |   }  | 
156  | 0  |   return dst;  | 
157  | 0  | }  | 
158  |  | #endif  | 
159  |  |  | 
160  |  | BOOL winpr_str_append(const char* what, char* buffer, size_t size, const char* separator)  | 
161  | 69.4k  | { | 
162  | 69.4k  |   const size_t used = strnlen(buffer, size);  | 
163  | 69.4k  |   const size_t add = strnlen(what, size);  | 
164  | 69.4k  |   const size_t sep_len = separator ? strnlen(separator, size) : 0;  | 
165  | 69.4k  |   const size_t sep = (used > 0) ? sep_len : 0;  | 
166  |  |  | 
167  | 69.4k  |   if (used + add + sep >= size)  | 
168  | 20  |     return FALSE;  | 
169  |  |  | 
170  | 69.4k  |   if ((used > 0) && (sep_len > 0))  | 
171  | 26.0k  |     strncat(buffer, separator, sep_len);  | 
172  |  |  | 
173  | 69.4k  |   strncat(buffer, what, add);  | 
174  | 69.4k  |   return TRUE;  | 
175  | 69.4k  | }  | 
176  |  |  | 
177  |  | WINPR_ATTR_FORMAT_ARG(3, 4)  | 
178  |  | int winpr_asprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, ...)  | 
179  | 79.6k  | { | 
180  | 79.6k  |   va_list ap;  | 
181  |  |  | 
182  | 79.6k  |   va_start(ap, templ);  | 
183  | 79.6k  |   int rc = winpr_vasprintf(s, slen, templ, ap);  | 
184  | 79.6k  |   va_end(ap);  | 
185  | 79.6k  |   return rc;  | 
186  | 79.6k  | }  | 
187  |  |  | 
188  |  | WINPR_ATTR_FORMAT_ARG(3, 0)  | 
189  |  | int winpr_vasprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, va_list oap)  | 
190  | 79.6k  | { | 
191  | 79.6k  |   va_list ap;  | 
192  |  |  | 
193  | 79.6k  |   *s = NULL;  | 
194  | 79.6k  |   *slen = 0;  | 
195  |  |  | 
196  | 79.6k  |   va_copy(ap, oap);  | 
197  | 79.6k  |   const int length = vsnprintf(NULL, 0, templ, ap);  | 
198  | 79.6k  |   va_end(ap);  | 
199  | 79.6k  |   if (length < 0)  | 
200  | 0  |     return length;  | 
201  |  |  | 
202  | 79.6k  |   char* str = calloc((size_t)length + 1ul, sizeof(char));  | 
203  | 79.6k  |   if (!str)  | 
204  | 0  |     return -1;  | 
205  |  |  | 
206  | 79.6k  |   va_copy(ap, oap);  | 
207  | 79.6k  |   const int plen = vsprintf(str, templ, ap);  | 
208  | 79.6k  |   va_end(ap);  | 
209  |  |  | 
210  | 79.6k  |   WINPR_ASSERT(length == plen);  | 
211  | 79.6k  |   *s = str;  | 
212  | 79.6k  |   *slen = (size_t)length;  | 
213  | 79.6k  |   return length;  | 
214  | 79.6k  | }  | 
215  |  |  | 
216  |  | #ifndef _WIN32  | 
217  |  |  | 
218  |  | char* _strdup(const char* strSource)  | 
219  | 217k  | { | 
220  | 217k  |   if (strSource == NULL)  | 
221  | 0  |     return NULL;  | 
222  |  |  | 
223  | 217k  |   char* strDestination = strdup(strSource);  | 
224  |  |  | 
225  | 217k  |   if (strDestination == NULL)  | 
226  | 217k  |     WLog_ERR(TAG, "strdup");  | 
227  |  |  | 
228  | 217k  |   return strDestination;  | 
229  | 217k  | }  | 
230  |  |  | 
231  |  | WCHAR* _wcsdup(const WCHAR* strSource)  | 
232  | 0  | { | 
233  | 0  |   if (!strSource)  | 
234  | 0  |     return NULL;  | 
235  |  |  | 
236  | 0  |   size_t len = _wcslen(strSource);  | 
237  | 0  |   WCHAR* strDestination = calloc(len + 1, sizeof(WCHAR));  | 
238  |  | 
  | 
239  | 0  |   if (strDestination != NULL)  | 
240  | 0  |     memcpy(strDestination, strSource, len * sizeof(WCHAR));  | 
241  |  | 
  | 
242  | 0  |   if (strDestination == NULL)  | 
243  | 0  |     WLog_ERR(TAG, "wcsdup");  | 
244  |  | 
  | 
245  | 0  |   return strDestination;  | 
246  | 0  | }  | 
247  |  |  | 
248  |  | WCHAR* _wcsncat(WCHAR* dst, const WCHAR* src, size_t sz)  | 
249  | 0  | { | 
250  | 0  |   WINPR_ASSERT(dst);  | 
251  | 0  |   WINPR_ASSERT(src || (sz == 0));  | 
252  |  |  | 
253  | 0  |   const size_t dlen = _wcslen(dst);  | 
254  | 0  |   const size_t slen = _wcsnlen(src, sz);  | 
255  | 0  |   for (size_t x = 0; x < slen; x++)  | 
256  | 0  |     dst[dlen + x] = src[x];  | 
257  | 0  |   dst[dlen + slen] = '\0';  | 
258  | 0  |   return dst;  | 
259  | 0  | }  | 
260  |  |  | 
261  |  | int _stricmp(const char* string1, const char* string2)  | 
262  | 40.0k  | { | 
263  | 40.0k  |   return strcasecmp(string1, string2);  | 
264  | 40.0k  | }  | 
265  |  |  | 
266  |  | int _strnicmp(const char* string1, const char* string2, size_t count)  | 
267  | 0  | { | 
268  | 0  |   return strncasecmp(string1, string2, count);  | 
269  | 0  | }  | 
270  |  |  | 
271  |  | /* _wcscmp -> wcscmp */  | 
272  |  |  | 
273  |  | int _wcscmp(const WCHAR* string1, const WCHAR* string2)  | 
274  | 0  | { | 
275  | 0  |   WINPR_ASSERT(string1);  | 
276  | 0  |   WINPR_ASSERT(string2);  | 
277  |  |  | 
278  | 0  |   while (TRUE)  | 
279  | 0  |   { | 
280  | 0  |     const WCHAR w1 = *string1++;  | 
281  | 0  |     const WCHAR w2 = *string2++;  | 
282  |  | 
  | 
283  | 0  |     if (w1 != w2)  | 
284  | 0  |       return (int)w1 - w2;  | 
285  | 0  |     else if ((w1 == '\0') || (w2 == '\0'))  | 
286  | 0  |       return (int)w1 - w2;  | 
287  | 0  |   }  | 
288  | 0  | }  | 
289  |  |  | 
290  |  | int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count)  | 
291  | 0  | { | 
292  | 0  |   WINPR_ASSERT(string1);  | 
293  | 0  |   WINPR_ASSERT(string2);  | 
294  |  |  | 
295  | 0  |   for (size_t x = 0; x < count; x++)  | 
296  | 0  |   { | 
297  | 0  |     const WCHAR a = string1[x];  | 
298  | 0  |     const WCHAR b = string2[x];  | 
299  |  | 
  | 
300  | 0  |     if (a != b)  | 
301  | 0  |       return (int)a - b;  | 
302  | 0  |     else if ((a == '\0') || (b == '\0'))  | 
303  | 0  |       return (int)a - b;  | 
304  | 0  |   }  | 
305  | 0  |   return 0;  | 
306  | 0  | }  | 
307  |  |  | 
308  |  | /* _wcslen -> wcslen */  | 
309  |  |  | 
310  |  | size_t _wcslen(const WCHAR* str)  | 
311  | 0  | { | 
312  | 0  |   const WCHAR* p = str;  | 
313  |  | 
  | 
314  | 0  |   WINPR_ASSERT(p);  | 
315  |  |  | 
316  | 0  |   while (*p)  | 
317  | 0  |     p++;  | 
318  |  | 
  | 
319  | 0  |   return (size_t)(p - str);  | 
320  | 0  | }  | 
321  |  |  | 
322  |  | /* _wcsnlen -> wcsnlen */  | 
323  |  |  | 
324  |  | size_t _wcsnlen(const WCHAR* str, size_t max)  | 
325  | 362k  | { | 
326  | 362k  |   WINPR_ASSERT(str);  | 
327  |  |  | 
328  | 362k  |   size_t x = 0;  | 
329  | 7.75M  |   for (; x < max; x++)  | 
330  | 7.71M  |   { | 
331  | 7.71M  |     if (str[x] == 0)  | 
332  | 328k  |       return x;  | 
333  | 7.71M  |   }  | 
334  |  |  | 
335  | 34.0k  |   return x;  | 
336  | 362k  | }  | 
337  |  |  | 
338  |  | /* _wcsstr -> wcsstr */  | 
339  |  |  | 
340  |  | WCHAR* _wcsstr(const WCHAR* str, const WCHAR* strSearch)  | 
341  | 0  | { | 
342  | 0  |   WINPR_ASSERT(str);  | 
343  | 0  |   WINPR_ASSERT(strSearch);  | 
344  |  |  | 
345  | 0  |   if (strSearch[0] == '\0')  | 
346  | 0  |     return (WCHAR*)str;  | 
347  |  |  | 
348  | 0  |   const size_t searchLen = _wcslen(strSearch);  | 
349  | 0  |   while (*str)  | 
350  | 0  |   { | 
351  | 0  |     if (_wcsncmp(str, strSearch, searchLen) == 0)  | 
352  | 0  |       return (WCHAR*)str;  | 
353  | 0  |     str++;  | 
354  | 0  |   }  | 
355  | 0  |   return NULL;  | 
356  | 0  | }  | 
357  |  |  | 
358  |  | /* _wcschr -> wcschr */  | 
359  |  |  | 
360  |  | WCHAR* _wcschr(const WCHAR* str, WCHAR value)  | 
361  | 0  | { | 
362  | 0  |   union  | 
363  | 0  |   { | 
364  | 0  |     const WCHAR* cc;  | 
365  | 0  |     WCHAR* c;  | 
366  | 0  |   } cnv;  | 
367  | 0  |   const WCHAR* p = str;  | 
368  |  | 
  | 
369  | 0  |   while (*p && (*p != value))  | 
370  | 0  |     p++;  | 
371  |  | 
  | 
372  | 0  |   cnv.cc = (*p == value) ? p : NULL;  | 
373  | 0  |   return cnv.c;  | 
374  | 0  | }  | 
375  |  |  | 
376  |  | /* _wcsrchr -> wcsrchr */  | 
377  |  |  | 
378  |  | WCHAR* _wcsrchr(const WCHAR* str, WCHAR c)  | 
379  | 0  | { | 
380  | 0  |   union  | 
381  | 0  |   { | 
382  | 0  |     const WCHAR* cc;  | 
383  | 0  |     WCHAR* c;  | 
384  | 0  |   } cnv;  | 
385  | 0  |   const WCHAR* p = NULL;  | 
386  |  | 
  | 
387  | 0  |   if (!str)  | 
388  | 0  |     return NULL;  | 
389  |  |  | 
390  | 0  |   for (; *str != '\0'; str++)  | 
391  | 0  |   { | 
392  | 0  |     const WCHAR ch = *str;  | 
393  | 0  |     if (ch == c)  | 
394  | 0  |       p = str;  | 
395  | 0  |   }  | 
396  |  | 
  | 
397  | 0  |   cnv.cc = p;  | 
398  | 0  |   return cnv.c;  | 
399  | 0  | }  | 
400  |  |  | 
401  |  | char* strtok_s(char* strToken, const char* strDelimit, char** context)  | 
402  | 0  | { | 
403  | 0  |   return strtok_r(strToken, strDelimit, context);  | 
404  | 0  | }  | 
405  |  |  | 
406  |  | WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context)  | 
407  | 0  | { | 
408  | 0  |   WCHAR* nextToken = NULL;  | 
409  | 0  |   WCHAR value = 0;  | 
410  |  | 
  | 
411  | 0  |   if (!strToken)  | 
412  | 0  |     strToken = *context;  | 
413  |  | 
  | 
414  | 0  |   value = *strToken;  | 
415  |  | 
  | 
416  | 0  |   while (*strToken && _wcschr(strDelimit, value))  | 
417  | 0  |   { | 
418  | 0  |     strToken++;  | 
419  | 0  |     value = *strToken;  | 
420  | 0  |   }  | 
421  |  | 
  | 
422  | 0  |   if (!*strToken)  | 
423  | 0  |     return NULL;  | 
424  |  |  | 
425  | 0  |   nextToken = strToken++;  | 
426  | 0  |   value = *strToken;  | 
427  |  | 
  | 
428  | 0  |   while (*strToken && !(_wcschr(strDelimit, value)))  | 
429  | 0  |   { | 
430  | 0  |     strToken++;  | 
431  | 0  |     value = *strToken;  | 
432  | 0  |   }  | 
433  |  | 
  | 
434  | 0  |   if (*strToken)  | 
435  | 0  |     *strToken++ = 0;  | 
436  |  | 
  | 
437  | 0  |   *context = strToken;  | 
438  | 0  |   return nextToken;  | 
439  | 0  | }  | 
440  |  |  | 
441  |  | #endif  | 
442  |  |  | 
443  |  | #if !defined(_WIN32) || defined(_UWP)  | 
444  |  |  | 
445  |  | /* Windows API Sets - api-ms-win-core-string-l2-1-0.dll  | 
446  |  |  * http://msdn.microsoft.com/en-us/library/hh802935/  | 
447  |  |  */  | 
448  |  |  | 
449  |  | #include "casing.h"  | 
450  |  |  | 
451  |  | LPSTR CharUpperA(LPSTR lpsz)  | 
452  | 0  | { | 
453  | 0  |   size_t length = 0;  | 
454  |  | 
  | 
455  | 0  |   if (!lpsz)  | 
456  | 0  |     return NULL;  | 
457  |  |  | 
458  | 0  |   length = strlen(lpsz);  | 
459  |  | 
  | 
460  | 0  |   if (length < 1)  | 
461  | 0  |     return (LPSTR)NULL;  | 
462  |  |  | 
463  | 0  |   if (length == 1)  | 
464  | 0  |   { | 
465  | 0  |     char c = *lpsz;  | 
466  |  | 
  | 
467  | 0  |     if ((c >= 'a') && (c <= 'z'))  | 
468  | 0  |       c = c - 'a' + 'A';  | 
469  |  | 
  | 
470  | 0  |     *lpsz = c;  | 
471  | 0  |     return lpsz;  | 
472  | 0  |   }  | 
473  |  |  | 
474  | 0  |   for (size_t i = 0; i < length; i++)  | 
475  | 0  |   { | 
476  | 0  |     if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))  | 
477  | 0  |       lpsz[i] = lpsz[i] - 'a' + 'A';  | 
478  | 0  |   }  | 
479  |  | 
  | 
480  | 0  |   return lpsz;  | 
481  | 0  | }  | 
482  |  |  | 
483  |  | LPWSTR CharUpperW(LPWSTR lpsz)  | 
484  | 0  | { | 
485  | 0  |   size_t length = 0;  | 
486  |  | 
  | 
487  | 0  |   if (!lpsz)  | 
488  | 0  |     return NULL;  | 
489  |  |  | 
490  | 0  |   length = _wcslen(lpsz);  | 
491  |  | 
  | 
492  | 0  |   if (length < 1)  | 
493  | 0  |     return (LPWSTR)NULL;  | 
494  |  |  | 
495  | 0  |   if (length == 1)  | 
496  | 0  |   { | 
497  | 0  |     WCHAR c = *lpsz;  | 
498  |  | 
  | 
499  | 0  |     if ((c >= L'a') && (c <= L'z'))  | 
500  | 0  |       c = c - L'a' + L'A';  | 
501  |  | 
  | 
502  | 0  |     *lpsz = c;  | 
503  | 0  |     return lpsz;  | 
504  | 0  |   }  | 
505  |  |  | 
506  | 0  |   for (size_t i = 0; i < length; i++)  | 
507  | 0  |   { | 
508  | 0  |     if ((lpsz[i] >= L'a') && (lpsz[i] <= L'z'))  | 
509  | 0  |       lpsz[i] = lpsz[i] - L'a' + L'A';  | 
510  | 0  |   }  | 
511  |  | 
  | 
512  | 0  |   return lpsz;  | 
513  | 0  | }  | 
514  |  |  | 
515  |  | DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength)  | 
516  | 0  | { | 
517  | 0  |   if (cchLength < 1)  | 
518  | 0  |     return 0;  | 
519  |  |  | 
520  | 0  |   for (DWORD i = 0; i < cchLength; i++)  | 
521  | 0  |   { | 
522  | 0  |     if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))  | 
523  | 0  |       lpsz[i] = lpsz[i] - 'a' + 'A';  | 
524  | 0  |   }  | 
525  |  | 
  | 
526  | 0  |   return cchLength;  | 
527  | 0  | }  | 
528  |  |  | 
529  |  | DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)  | 
530  | 0  | { | 
531  | 0  |   WCHAR value = 0;  | 
532  |  | 
  | 
533  | 0  |   for (DWORD i = 0; i < cchLength; i++)  | 
534  | 0  |   { | 
535  | 0  |     Data_Read_UINT16(&lpsz[i], value);  | 
536  | 0  |     value = WINPR_TOUPPERW(value);  | 
537  | 0  |     Data_Write_UINT16(&lpsz[i], value);  | 
538  | 0  |   }  | 
539  |  | 
  | 
540  | 0  |   return cchLength;  | 
541  | 0  | }  | 
542  |  |  | 
543  |  | LPSTR CharLowerA(LPSTR lpsz)  | 
544  | 0  | { | 
545  | 0  |   size_t length = 0;  | 
546  |  | 
  | 
547  | 0  |   if (!lpsz)  | 
548  | 0  |     return (LPSTR)NULL;  | 
549  |  |  | 
550  | 0  |   length = strlen(lpsz);  | 
551  |  | 
  | 
552  | 0  |   if (length < 1)  | 
553  | 0  |     return (LPSTR)NULL;  | 
554  |  |  | 
555  | 0  |   if (length == 1)  | 
556  | 0  |   { | 
557  | 0  |     char c = *lpsz;  | 
558  |  | 
  | 
559  | 0  |     if ((c >= 'A') && (c <= 'Z'))  | 
560  | 0  |       c = c - 'A' + 'a';  | 
561  |  | 
  | 
562  | 0  |     *lpsz = c;  | 
563  | 0  |     return lpsz;  | 
564  | 0  |   }  | 
565  |  |  | 
566  | 0  |   for (size_t i = 0; i < length; i++)  | 
567  | 0  |   { | 
568  | 0  |     if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))  | 
569  | 0  |       lpsz[i] = lpsz[i] - 'A' + 'a';  | 
570  | 0  |   }  | 
571  |  | 
  | 
572  | 0  |   return lpsz;  | 
573  | 0  | }  | 
574  |  |  | 
575  |  | LPWSTR CharLowerW(LPWSTR lpsz)  | 
576  | 0  | { | 
577  | 0  |   CharLowerBuffW(lpsz, _wcslen(lpsz));  | 
578  | 0  |   return lpsz;  | 
579  | 0  | }  | 
580  |  |  | 
581  |  | DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength)  | 
582  | 0  | { | 
583  | 0  |   if (cchLength < 1)  | 
584  | 0  |     return 0;  | 
585  |  |  | 
586  | 0  |   for (DWORD i = 0; i < cchLength; i++)  | 
587  | 0  |   { | 
588  | 0  |     if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))  | 
589  | 0  |       lpsz[i] = lpsz[i] - 'A' + 'a';  | 
590  | 0  |   }  | 
591  |  | 
  | 
592  | 0  |   return cchLength;  | 
593  | 0  | }  | 
594  |  |  | 
595  |  | DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)  | 
596  | 0  | { | 
597  | 0  |   WCHAR value = 0;  | 
598  |  | 
  | 
599  | 0  |   for (DWORD i = 0; i < cchLength; i++)  | 
600  | 0  |   { | 
601  | 0  |     Data_Read_UINT16(&lpsz[i], value);  | 
602  | 0  |     value = WINPR_TOLOWERW(value);  | 
603  | 0  |     Data_Write_UINT16(&lpsz[i], value);  | 
604  | 0  |   }  | 
605  |  | 
  | 
606  | 0  |   return cchLength;  | 
607  | 0  | }  | 
608  |  |  | 
609  |  | BOOL IsCharAlphaA(CHAR ch)  | 
610  | 0  | { | 
611  | 0  |   if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')))  | 
612  | 0  |     return 1;  | 
613  | 0  |   else  | 
614  | 0  |     return 0;  | 
615  | 0  | }  | 
616  |  |  | 
617  |  | BOOL IsCharAlphaW(WCHAR ch)  | 
618  | 0  | { | 
619  | 0  |   if (((ch >= L'a') && (ch <= L'z')) || ((ch >= L'A') && (ch <= L'Z')))  | 
620  | 0  |     return 1;  | 
621  | 0  |   else  | 
622  | 0  |     return 0;  | 
623  | 0  | }  | 
624  |  |  | 
625  |  | BOOL IsCharAlphaNumericA(CHAR ch)  | 
626  | 0  | { | 
627  | 0  |   if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||  | 
628  | 0  |       ((ch >= '0') && (ch <= '9')))  | 
629  | 0  |     return 1;  | 
630  | 0  |   else  | 
631  | 0  |     return 0;  | 
632  | 0  | }  | 
633  |  |  | 
634  |  | BOOL IsCharAlphaNumericW(WCHAR ch)  | 
635  | 0  | { | 
636  | 0  |   if (((ch >= L'a') && (ch <= L'z')) || ((ch >= L'A') && (ch <= L'Z')) ||  | 
637  | 0  |       ((ch >= L'0') && (ch <= L'9')))  | 
638  | 0  |     return 1;  | 
639  | 0  |   else  | 
640  | 0  |     return 0;  | 
641  | 0  | }  | 
642  |  |  | 
643  |  | BOOL IsCharUpperA(CHAR ch)  | 
644  | 0  | { | 
645  | 0  |   if ((ch >= 'A') && (ch <= 'Z'))  | 
646  | 0  |     return 1;  | 
647  | 0  |   else  | 
648  | 0  |     return 0;  | 
649  | 0  | }  | 
650  |  |  | 
651  |  | BOOL IsCharUpperW(WCHAR ch)  | 
652  | 0  | { | 
653  | 0  |   if ((ch >= L'A') && (ch <= L'Z'))  | 
654  | 0  |     return 1;  | 
655  | 0  |   else  | 
656  | 0  |     return 0;  | 
657  | 0  | }  | 
658  |  |  | 
659  |  | BOOL IsCharLowerA(CHAR ch)  | 
660  | 0  | { | 
661  | 0  |   if ((ch >= 'a') && (ch <= 'z'))  | 
662  | 0  |     return 1;  | 
663  | 0  |   else  | 
664  | 0  |     return 0;  | 
665  | 0  | }  | 
666  |  |  | 
667  |  | BOOL IsCharLowerW(WCHAR ch)  | 
668  | 0  | { | 
669  | 0  |   if ((ch >= L'a') && (ch <= L'z'))  | 
670  | 0  |     return 1;  | 
671  | 0  |   else  | 
672  | 0  |     return 0;  | 
673  | 0  | }  | 
674  |  |  | 
675  |  | #endif  | 
676  |  |  | 
677  |  | size_t ConvertLineEndingToLF(char* str, size_t size)  | 
678  | 0  | { | 
679  | 0  |   size_t skip = 0;  | 
680  |  | 
  | 
681  | 0  |   WINPR_ASSERT(str || (size == 0));  | 
682  | 0  |   for (size_t x = 0; x < size; x++)  | 
683  | 0  |   { | 
684  | 0  |     char c = str[x];  | 
685  | 0  |     switch (c)  | 
686  | 0  |     { | 
687  | 0  |       case '\r':  | 
688  | 0  |         str[x - skip] = '\n';  | 
689  | 0  |         if ((x + 1 < size) && (str[x + 1] == '\n'))  | 
690  | 0  |           skip++;  | 
691  | 0  |         break;  | 
692  | 0  |       default:  | 
693  | 0  |         str[x - skip] = c;  | 
694  | 0  |         break;  | 
695  | 0  |     }  | 
696  | 0  |   }  | 
697  | 0  |   return size - skip;  | 
698  | 0  | }  | 
699  |  |  | 
700  |  | char* ConvertLineEndingToCRLF(const char* str, size_t* size)  | 
701  | 0  | { | 
702  | 0  |   WINPR_ASSERT(size);  | 
703  | 0  |   const size_t s = *size;  | 
704  | 0  |   WINPR_ASSERT(str || (s == 0));  | 
705  |  |  | 
706  | 0  |   *size = 0;  | 
707  | 0  |   if (s == 0)  | 
708  | 0  |     return NULL;  | 
709  |  |  | 
710  | 0  |   size_t linebreaks = 0;  | 
711  | 0  |   for (size_t x = 0; x < s - 1; x++)  | 
712  | 0  |   { | 
713  | 0  |     char c = str[x];  | 
714  | 0  |     switch (c)  | 
715  | 0  |     { | 
716  | 0  |       case '\r':  | 
717  | 0  |       case '\n':  | 
718  | 0  |         linebreaks++;  | 
719  | 0  |         break;  | 
720  | 0  |       default:  | 
721  | 0  |         break;  | 
722  | 0  |     }  | 
723  | 0  |   }  | 
724  | 0  |   char* cnv = calloc(s + linebreaks * 2ull + 1ull, sizeof(char));  | 
725  | 0  |   if (!cnv)  | 
726  | 0  |     return NULL;  | 
727  |  |  | 
728  | 0  |   size_t pos = 0;  | 
729  | 0  |   for (size_t x = 0; x < s; x++)  | 
730  | 0  |   { | 
731  | 0  |     const char c = str[x];  | 
732  | 0  |     switch (c)  | 
733  | 0  |     { | 
734  | 0  |       case '\r':  | 
735  | 0  |         cnv[pos++] = '\r';  | 
736  | 0  |         cnv[pos++] = '\n';  | 
737  | 0  |         break;  | 
738  | 0  |       case '\n':  | 
739  |  |         /* Do not duplicate existing \r\n sequences */  | 
740  | 0  |         if ((x > 0) && (str[x - 1] != '\r'))  | 
741  | 0  |         { | 
742  | 0  |           cnv[pos++] = '\r';  | 
743  | 0  |           cnv[pos++] = '\n';  | 
744  | 0  |         }  | 
745  | 0  |         break;  | 
746  | 0  |       default:  | 
747  | 0  |         cnv[pos++] = c;  | 
748  | 0  |         break;  | 
749  | 0  |     }  | 
750  | 0  |   }  | 
751  | 0  |   *size = pos;  | 
752  | 0  |   return cnv;  | 
753  | 0  | }  | 
754  |  |  | 
755  |  | char* StrSep(char** stringp, const char* delim)  | 
756  | 0  | { | 
757  | 0  |   char* start = *stringp;  | 
758  | 0  |   char* p = NULL;  | 
759  | 0  |   p = (start != NULL) ? strpbrk(start, delim) : NULL;  | 
760  |  | 
  | 
761  | 0  |   if (!p)  | 
762  | 0  |     *stringp = NULL;  | 
763  | 0  |   else  | 
764  | 0  |   { | 
765  | 0  |     *p = '\0';  | 
766  | 0  |     *stringp = p + 1;  | 
767  | 0  |   }  | 
768  |  | 
  | 
769  | 0  |   return start;  | 
770  | 0  | }  | 
771  |  |  | 
772  |  | INT64 GetLine(char** lineptr, size_t* size, FILE* stream)  | 
773  | 0  | { | 
774  |  | #if defined(_WIN32)  | 
775  |  |   char c;  | 
776  |  |   char* n;  | 
777  |  |   size_t step = 32;  | 
778  |  |   size_t used = 0;  | 
779  |  |  | 
780  |  |   if (!lineptr || !size)  | 
781  |  |   { | 
782  |  |     errno = EINVAL;  | 
783  |  |     return -1;  | 
784  |  |   }  | 
785  |  |  | 
786  |  |   do  | 
787  |  |   { | 
788  |  |     if (used + 2 >= *size)  | 
789  |  |     { | 
790  |  |       *size += step;  | 
791  |  |       n = realloc(*lineptr, *size);  | 
792  |  |  | 
793  |  |       if (!n)  | 
794  |  |       { | 
795  |  |         return -1;  | 
796  |  |       }  | 
797  |  |  | 
798  |  |       *lineptr = n;  | 
799  |  |     }  | 
800  |  |  | 
801  |  |     c = fgetc(stream);  | 
802  |  |  | 
803  |  |     if (c != EOF)  | 
804  |  |       (*lineptr)[used++] = c;  | 
805  |  |   } while ((c != '\n') && (c != '\r') && (c != EOF));  | 
806  |  |  | 
807  |  |   (*lineptr)[used] = '\0';  | 
808  |  |   return used;  | 
809  |  | #elif !defined(ANDROID) && !defined(IOS)  | 
810  |  |   return getline(lineptr, size, stream);  | 
811  |  | #else  | 
812  |  |   return -1;  | 
813  |  | #endif  | 
814  | 0  | }  | 
815  |  |  | 
816  |  | #if !defined(WINPR_HAVE_STRNDUP)  | 
817  |  | char* strndup(const char* src, size_t n)  | 
818  |  | { | 
819  |  |   char* dst = calloc(n + 1, sizeof(char));  | 
820  |  |   if (dst)  | 
821  |  |     strncpy(dst, src, n);  | 
822  |  |   return dst;  | 
823  |  | }  | 
824  |  | #endif  | 
825  |  |  | 
826  |  | const WCHAR* InitializeConstWCharFromUtf8(const char* str, WCHAR* buffer, size_t len)  | 
827  | 0  | { | 
828  | 0  |   WINPR_ASSERT(str);  | 
829  | 0  |   WINPR_ASSERT(buffer || (len == 0));  | 
830  | 0  |   ConvertUtf8ToWChar(str, buffer, len);  | 
831  | 0  |   return buffer;  | 
832  | 0  | }  |