1 /*
2 * Kernel string functions
3 *
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 2001 Dmitry Timoshkov for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <string.h>
29
30 #define WINE_NO_INLINE_STRING
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wine/winbase16.h"
34 #include "wine/unicode.h"
35 #include "wine/exception.h"
36
37
38 static INT (WINAPI *pLoadStringA)(HINSTANCE, UINT, LPSTR, INT);
39 static INT (WINAPI *pwvsprintfA)(LPSTR, LPCSTR, va_list);
40
41
42 /***********************************************************************
43 * Helper for k32 family functions
44 */
45 static void *user32_proc_address(const char *proc_name)
46 {
47 static HMODULE hUser32;
48
49 if(!hUser32) hUser32 = LoadLibraryA("user32.dll");
50 return GetProcAddress(hUser32, proc_name);
51 }
52
53
54 /***********************************************************************
55 * k32CharToOemBuffA (KERNEL32.11)
56 */
57 BOOL WINAPI k32CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
58 {
59 WCHAR *bufW;
60
61 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
62 {
63 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
64 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
65 HeapFree( GetProcessHeap(), 0, bufW );
66 }
67 return TRUE;
68 }
69
70
71 /***********************************************************************
72 * k32CharToOemA (KERNEL32.10)
73 */
74 BOOL WINAPI k32CharToOemA(LPCSTR s, LPSTR d)
75 {
76 if (!s || !d) return TRUE;
77 return k32CharToOemBuffA( s, d, strlen(s) + 1 );
78 }
79
80
81 /***********************************************************************
82 * k32OemToCharBuffA (KERNEL32.13)
83 */
84 BOOL WINAPI k32OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
85 {
86 WCHAR *bufW;
87
88 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
89 {
90 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
91 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
92 HeapFree( GetProcessHeap(), 0, bufW );
93 }
94 return TRUE;
95 }
96
97
98 /***********************************************************************
99 * k32OemToCharA (KERNEL32.12)
100 */
101 BOOL WINAPI k32OemToCharA(LPCSTR s, LPSTR d)
102 {
103 return k32OemToCharBuffA( s, d, strlen(s) + 1 );
104 }
105
106
107 /**********************************************************************
108 * k32LoadStringA (KERNEL32.14)
109 */
110 INT WINAPI k32LoadStringA(HINSTANCE instance, UINT resource_id,
111 LPSTR buffer, INT buflen)
112 {
113 if(!pLoadStringA) pLoadStringA = user32_proc_address("LoadStringA");
114 return pLoadStringA(instance, resource_id, buffer, buflen);
115 }
116
117
118 /***********************************************************************
119 * k32wvsprintfA (KERNEL32.16)
120 */
121 INT WINAPI k32wvsprintfA(LPSTR buffer, LPCSTR spec, va_list args)
122 {
123 if(!pwvsprintfA) pwvsprintfA = user32_proc_address("wvsprintfA");
124 return (*pwvsprintfA)(buffer, spec, args);
125 }
126
127
128 /***********************************************************************
129 * k32wsprintfA (KERNEL32.15)
130 */
131 INT WINAPIV k32wsprintfA(LPSTR buffer, LPCSTR spec, ...)
132 {
133 va_list args;
134 INT res;
135
136 va_start(args, spec);
137 res = k32wvsprintfA(buffer, spec, args);
138 va_end(args);
139 return res;
140 }
141
142
143 /***********************************************************************
144 * hmemcpy (KERNEL.348)
145 */
146 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
147 {
148 memcpy( dst, src, count );
149 }
150
151
152 /***********************************************************************
153 * lstrcat (KERNEL.89)
154 */
155 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
156 {
157 /* Windows does not check for NULL pointers here, so we don't either */
158 strcat( MapSL(dst), src );
159 return dst;
160 }
161
162
163 /***********************************************************************
164 * lstrcatA (KERNEL32.@)
165 * lstrcat (KERNEL32.@)
166 */
167 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
168 {
169 __TRY
170 {
171 strcat( dst, src );
172 }
173 __EXCEPT_PAGE_FAULT
174 {
175 SetLastError( ERROR_INVALID_PARAMETER );
176 return NULL;
177 }
178 __ENDTRY
179 return dst;
180 }
181
182
183 /***********************************************************************
184 * lstrcatW (KERNEL32.@)
185 */
186 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
187 {
188 __TRY
189 {
190 strcatW( dst, src );
191 }
192 __EXCEPT_PAGE_FAULT
193 {
194 SetLastError( ERROR_INVALID_PARAMETER );
195 return NULL;
196 }
197 __ENDTRY
198 return dst;
199 }
200
201
202 /***********************************************************************
203 * lstrcatn (KERNEL.352)
204 */
205 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
206 {
207 LPSTR p = MapSL(dst);
208 LPSTR start = p;
209
210 while (*p) p++;
211 if ((n -= (p - start)) <= 0) return dst;
212 lstrcpynA( p, src, n );
213 return dst;
214 }
215
216
217 /***********************************************************************
218 * lstrcpy (KERNEL.88)
219 */
220 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
221 {
222 if (!lstrcpyA( MapSL(dst), src )) dst = 0;
223 return dst;
224 }
225
226
227 /***********************************************************************
228 * lstrcpyA (KERNEL32.@)
229 * lstrcpy (KERNEL32.@)
230 */
231 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
232 {
233 __TRY
234 {
235 /* this is how Windows does it */
236 memmove( dst, src, strlen(src)+1 );
237 }
238 __EXCEPT_PAGE_FAULT
239 {
240 SetLastError( ERROR_INVALID_PARAMETER );
241 return NULL;
242 }
243 __ENDTRY
244 return dst;
245 }
246
247
248 /***********************************************************************
249 * lstrcpyW (KERNEL32.@)
250 */
251 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
252 {
253 __TRY
254 {
255 strcpyW( dst, src );
256 }
257 __EXCEPT_PAGE_FAULT
258 {
259 SetLastError( ERROR_INVALID_PARAMETER );
260 return NULL;
261 }
262 __ENDTRY
263 return dst;
264 }
265
266
267 /***********************************************************************
268 * lstrcpyn (KERNEL.353)
269 */
270 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
271 {
272 lstrcpynA( MapSL(dst), src, n );
273 return dst;
274 }
275
276
277 /***********************************************************************
278 * lstrcpynA (KERNEL32.@)
279 * lstrcpyn (KERNEL32.@)
280 *
281 * Note: this function differs from the UNIX strncpy, it _always_ writes
282 * a terminating \0.
283 *
284 * Note: n is an INT but Windows treats it as unsigned, and will happily
285 * copy a gazillion chars if n is negative.
286 */
287 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
288 {
289 __TRY
290 {
291 LPSTR d = dst;
292 LPCSTR s = src;
293 UINT count = n;
294
295 while ((count > 1) && *s)
296 {
297 count--;
298 *d++ = *s++;
299 }
300 if (count) *d = 0;
301 }
302 __EXCEPT_PAGE_FAULT
303 {
304 SetLastError( ERROR_INVALID_PARAMETER );
305 return 0;
306 }
307 __ENDTRY
308 return dst;
309 }
310
311
312 /***********************************************************************
313 * lstrcpynW (KERNEL32.@)
314 *
315 * Note: this function differs from the UNIX strncpy, it _always_ writes
316 * a terminating \0
317 *
318 * Note: n is an INT but Windows treats it as unsigned, and will happily
319 * copy a gazillion chars if n is negative.
320 */
321 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
322 {
323 __TRY
324 {
325 LPWSTR d = dst;
326 LPCWSTR s = src;
327 UINT count = n;
328
329 while ((count > 1) && *s)
330 {
331 count--;
332 *d++ = *s++;
333 }
334 if (count) *d = 0;
335 }
336 __EXCEPT_PAGE_FAULT
337 {
338 SetLastError( ERROR_INVALID_PARAMETER );
339 return 0;
340 }
341 __ENDTRY
342 return dst;
343 }
344
345
346 /***********************************************************************
347 * lstrlen (KERNEL.90)
348 */
349 INT16 WINAPI lstrlen16( LPCSTR str )
350 {
351 return (INT16)lstrlenA( str );
352 }
353
354
355 /***********************************************************************
356 * lstrlenA (KERNEL32.@)
357 * lstrlen (KERNEL32.@)
358 */
359 INT WINAPI lstrlenA( LPCSTR str )
360 {
361 INT ret;
362 __TRY
363 {
364 ret = strlen(str);
365 }
366 __EXCEPT_PAGE_FAULT
367 {
368 SetLastError( ERROR_INVALID_PARAMETER );
369 return 0;
370 }
371 __ENDTRY
372 return ret;
373 }
374
375
376 /***********************************************************************
377 * lstrlenW (KERNEL32.@)
378 */
379 INT WINAPI lstrlenW( LPCWSTR str )
380 {
381 INT ret;
382 __TRY
383 {
384 ret = strlenW(str);
385 }
386 __EXCEPT_PAGE_FAULT
387 {
388 SetLastError( ERROR_INVALID_PARAMETER );
389 return 0;
390 }
391 __ENDTRY
392 return ret;
393 }
394
395
396 /***********************************************************************
397 * UnicodeToAnsi (KERNEL.434)
398 */
399 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
400 {
401 if ( codepage == -1 ) codepage = CP_ACP;
402 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
403 }
404
405
406 /***************************************************************************
407 *
408 * Win 2.x string functions now moved to USER
409 *
410 * We rather want to implement them here instead of doing Callouts
411 */
412
413 /***********************************************************************
414 * Reserved1 (KERNEL.77)
415 */
416 SEGPTR WINAPI KERNEL_AnsiNext16(SEGPTR current)
417 {
418 return (*(char *)MapSL(current)) ? current + 1 : current;
419 }
420
421 /***********************************************************************
422 * Reserved2(KERNEL.78)
423 */
424 SEGPTR WINAPI KERNEL_AnsiPrev16( SEGPTR start, SEGPTR current )
425 {
426 return (current==start)?start:current-1;
427 }
428
429 /***********************************************************************
430 * Reserved3 (KERNEL.79)
431 */
432 SEGPTR WINAPI KERNEL_AnsiUpper16( SEGPTR strOrChar )
433 {
434 /* uppercase only one char if strOrChar < 0x10000 */
435 if (HIWORD(strOrChar))
436 {
437 char *s = MapSL(strOrChar);
438 while (*s)
439 {
440 *s = toupper(*s);
441 s++;
442 }
443 return strOrChar;
444 }
445 else return toupper((char)strOrChar);
446 }
447
448 /***********************************************************************
449 * Reserved4 (KERNEL.80)
450 */
451 SEGPTR WINAPI KERNEL_AnsiLower16( SEGPTR strOrChar )
452 {
453 /* lowercase only one char if strOrChar < 0x10000 */
454 if (HIWORD(strOrChar))
455 {
456 char *s = MapSL(strOrChar);
457 while (*s)
458 {
459 *s = tolower(*s);
460 s++;
461 }
462 return strOrChar;
463 }
464 else return tolower((char)strOrChar);
465 }
466
467
468 /***********************************************************************
469 * Reserved5 (KERNEL.87)
470 */
471 INT16 WINAPI KERNEL_lstrcmp16( LPCSTR str1, LPCSTR str2 )
472 {
473 return (INT16)strcmp( str1, str2 );
474 }
475
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.