Fixed some issues found by winapi_check.
[wine] / dlls / user / lstr.c
1 /*
2  * USER string functions
3  *
4  * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 1996 Marcus Meissner
7  */
8
9 #include <ctype.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #include "windef.h"
15 #include "winbase.h"
16 #include "winnls.h"
17 #include "wine/winbase16.h"
18 #include "wine/winuser16.h"
19 #include "wine/unicode.h"
20
21 #include "heap.h"
22 #include "debugtools.h"
23
24 DEFAULT_DEBUG_CHANNEL(resource);
25
26 /***********************************************************************
27  *           AnsiToOem16   (KEYBOARD.5)
28  */
29 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
30 {
31     CharToOemA( s, d );
32     return -1;
33 }
34
35
36 /***********************************************************************
37  *           OemToAnsi16   (KEYBOARD.6)
38  */
39 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
40 {
41     OemToCharA( s, d );
42     return -1;
43 }
44
45
46 /***********************************************************************
47  *           AnsiToOemBuff16   (KEYBOARD.134)
48  */
49 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
50 {
51     if (len != 0) CharToOemBuffA( s, d, len );
52 }
53
54
55 /***********************************************************************
56  *           OemToAnsiBuff16   (KEYBOARD.135)
57  */
58 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
59 {
60     if (len != 0) OemToCharBuffA( s, d, len );
61 }
62
63
64 /***********************************************************************
65  *           lstrcmp16   (USER.430)
66  */
67 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
68 {
69     return (INT16)strcmp( str1, str2 );
70 }
71
72
73 /***********************************************************************
74  *           AnsiUpper16   (USER.431)
75  */
76 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
77 {
78     /* uppercase only one char if strOrChar < 0x10000 */
79     if (HIWORD(strOrChar))
80     {
81         CharUpperA( MapSL(strOrChar) );
82         return strOrChar;
83     }
84     else return toupper((char)strOrChar);
85 }
86
87
88 /***********************************************************************
89  *           AnsiLower16   (USER.432)
90  */
91 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
92 {
93     /* lowercase only one char if strOrChar < 0x10000 */
94     if (HIWORD(strOrChar))
95     {
96         CharLowerA( MapSL(strOrChar) );
97         return strOrChar;
98     }
99     else return tolower((char)strOrChar);
100 }
101
102
103 /***********************************************************************
104  *           AnsiUpperBuff16   (USER.437)
105  */
106 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
107 {
108     CharUpperBuffA( str, len ? len : 65536 );
109     return len;
110 }
111
112
113 /***********************************************************************
114  *           AnsiLowerBuff16   (USER.438)
115  */
116 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
117 {
118     CharLowerBuffA( str, len ? len : 65536 );
119     return len;
120 }
121
122
123 /***********************************************************************
124  *           AnsiNext16   (USER.472)
125  */
126 SEGPTR WINAPI AnsiNext16(SEGPTR current)
127 {
128     char *ptr = MapSL(current);
129     return current + (CharNextA(ptr) - ptr);
130 }
131
132
133 /***********************************************************************
134  *           AnsiPrev16   (USER.473)
135  */
136 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
137 {
138     char *ptr = MapSL(current);
139     return current - (ptr - CharPrevA( start, ptr ));
140 }
141
142
143 /***********************************************************************
144  *           CharNextA   (USER32.@)
145  */
146 LPSTR WINAPI CharNextA( LPCSTR ptr )
147 {
148     if (!*ptr) return (LPSTR)ptr;
149     if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
150     return (LPSTR)(ptr + 1);
151 }
152
153
154 /***********************************************************************
155  *           CharNextExA   (USER32.@)
156  */
157 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
158 {
159     if (!*ptr) return (LPSTR)ptr;
160     if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
161     return (LPSTR)(ptr + 1);
162 }
163
164
165 /***********************************************************************
166  *           CharNextExW   (USER32.@)
167  */
168 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
169 {
170     /* doesn't make sense, there are no codepages for Unicode */
171     return NULL;
172 }
173
174
175 /***********************************************************************
176  *           CharNextW   (USER32.@)
177  */
178 LPWSTR WINAPI CharNextW(LPCWSTR x)
179 {
180     if (*x) x++;
181
182     return (LPWSTR)x;
183 }
184
185
186 /***********************************************************************
187  *           CharPrevA   (USER32.@)
188  */
189 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
190 {
191     while (*start && (start < ptr))
192     {
193         LPCSTR next = CharNextA( start );
194         if (next >= ptr) break;
195         start = next;
196     }
197     return (LPSTR)start;
198 }
199
200
201 /***********************************************************************
202  *           CharPrevExA   (USER32.@)
203  */
204 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
205 {
206     while (*start && (start < ptr))
207     {
208         LPCSTR next = CharNextExA( codepage, start, flags );
209         if (next > ptr) break;
210         start = next;
211     }
212     return (LPSTR)start;
213 }
214
215
216 /***********************************************************************
217  *           CharPrevExW   (USER32.@)
218  */
219 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
220 {
221     /* doesn't make sense, there are no codepages for Unicode */
222     return NULL;
223 }
224
225
226 /***********************************************************************
227  *           CharPrevW   (USER32.@)
228  */
229 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
230 {
231     if (x>start) return (LPWSTR)(x-1);
232     else return (LPWSTR)x;
233 }
234
235
236 /***********************************************************************
237  *           CharToOemA   (USER32.@)
238  */
239 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
240 {
241     if ( !s || !d ) return TRUE;
242     return CharToOemBuffA( s, d, strlen( s ) + 1 );
243 }
244
245
246 /***********************************************************************
247  *           CharToOemBuffA   (USER32.@)
248  */
249 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
250 {
251     WCHAR *bufW;
252
253     bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
254     if( bufW )
255     {
256         MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
257         WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
258         HeapFree( GetProcessHeap(), 0, bufW );
259     }
260     return TRUE;
261 }
262
263
264 /***********************************************************************
265  *           CharToOemBuffW   (USER32.@)
266  */
267 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
268 {
269    if ( !s || !d ) return TRUE;
270     WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
271     return TRUE;
272 }
273
274
275 /***********************************************************************
276  *           CharToOemW   (USER32.@)
277  */
278 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
279 {
280     return CharToOemBuffW( s, d, strlenW( s ) + 1 );
281 }
282
283
284 /***********************************************************************
285  *           OemToCharA   (USER32.@)
286  */
287 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
288 {
289     return OemToCharBuffA( s, d, strlen( s ) + 1 );
290 }
291
292
293 /***********************************************************************
294  *           OemToCharBuffA   (USER32.@)
295  */
296 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
297 {
298     WCHAR *bufW;
299
300     bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
301     if( bufW )
302     {
303         MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
304         WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
305         HeapFree( GetProcessHeap(), 0, bufW );
306     }
307     return TRUE;
308 }
309
310
311 /***********************************************************************
312  *           OemToCharBuffW   (USER32.@)
313  */
314 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
315 {
316     MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
317     return TRUE;
318 }
319
320
321 /***********************************************************************
322  *           OemToCharW   (USER32.@)
323  */
324 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
325 {
326     return OemToCharBuffW( s, d, strlen( s ) + 1 );
327 }
328
329
330 /***********************************************************************
331  *           CharLowerA   (USER32.25)
332  * FIXME: handle current locale
333  */
334 LPSTR WINAPI CharLowerA(LPSTR x)
335 {
336     LPSTR       s;
337
338     if (HIWORD(x))
339     {
340         s=x;
341         while (*s)
342         {
343             *s=tolower(*s);
344             s++;
345         }
346         return x;
347     }
348     else return (LPSTR)tolower((char)(int)x);
349 }
350
351
352 /***********************************************************************
353  *           CharUpperA   (USER32.@)
354  * FIXME: handle current locale
355  */
356 LPSTR WINAPI CharUpperA(LPSTR x)
357 {
358     if (HIWORD(x))
359     {
360         LPSTR s = x;
361         while (*s)
362         {
363             *s=toupper(*s);
364             s++;
365         }
366         return x;
367     }
368     return (LPSTR)toupper((char)(int)x);
369 }
370
371
372 /***********************************************************************
373  *           CharLowerW   (USER32.@)
374  */
375 LPWSTR WINAPI CharLowerW(LPWSTR x)
376 {
377     if (HIWORD(x)) return strlwrW(x);
378     else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
379 }
380
381
382 /***********************************************************************
383  *           CharUpperW   (USER32.@)
384  * FIXME: handle current locale
385  */
386 LPWSTR WINAPI CharUpperW(LPWSTR x)
387 {
388     if (HIWORD(x)) return struprW(x);
389     else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
390 }
391
392
393 /***********************************************************************
394  *           CharLowerBuffA   (USER32.@)
395  * FIXME: handle current locale
396  */
397 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
398 {
399     DWORD ret = len;
400     if (!str) return 0; /* YES */
401     for (; len; len--, str++) *str = tolower(*str);
402     return ret;
403 }
404
405
406 /***********************************************************************
407  *           CharLowerBuffW   (USER32.@)
408  */
409 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
410 {
411     DWORD ret = len;
412     if (!str) return 0; /* YES */
413     for (; len; len--, str++) *str = tolowerW(*str);
414     return ret;
415 }
416
417
418 /***********************************************************************
419  *           CharUpperBuffA   (USER32.@)
420  * FIXME: handle current locale
421  */
422 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
423 {
424     DWORD ret = len;
425     if (!str) return 0; /* YES */
426     for (; len; len--, str++) *str = toupper(*str);
427     return ret;
428 }
429
430
431 /***********************************************************************
432  *           CharUpperBuffW   (USER32.@)
433  */
434 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
435 {
436     DWORD ret = len;
437     if (!str) return 0; /* YES */
438     for (; len; len--, str++) *str = toupperW(*str);
439     return ret;
440 }
441
442
443 /***********************************************************************
444  *           IsCharLowerA   (USER.436) (USER32.@)
445  * FIXME: handle current locale
446  */
447 BOOL WINAPI IsCharLowerA(CHAR x)
448 {
449     return islower(x);
450 }
451
452
453 /***********************************************************************
454  *           IsCharLowerW   (USER32.@)
455  */
456 BOOL WINAPI IsCharLowerW(WCHAR x)
457 {
458     return get_char_typeW(x) & C1_LOWER;
459 }
460
461
462 /***********************************************************************
463  *           IsCharUpperA   (USER.435) (USER32.337)
464  * FIXME: handle current locale
465  */
466 BOOL WINAPI IsCharUpperA(CHAR x)
467 {
468     return isupper(x);
469 }
470
471
472 /***********************************************************************
473  *           IsCharUpperW   (USER32.@)
474  */
475 BOOL WINAPI IsCharUpperW(WCHAR x)
476 {
477     return get_char_typeW(x) & C1_UPPER;
478 }
479
480
481 /***********************************************************************
482  *           IsCharAlphaNumericW   (USER32.@)
483  */
484 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
485 {
486     return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
487 }
488
489
490 /***********************************************************************
491  *           IsCharAlphaW   (USER32.@)
492  */
493 BOOL WINAPI IsCharAlphaW(WCHAR x)
494 {
495     return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
496 }
497
498
499 /***********************************************************************
500  *           FormatMessage16   (USER.606)
501  */
502 DWORD WINAPI FormatMessage16(
503     DWORD   dwFlags,
504     SEGPTR lpSource,     /* [in] NOTE: not always a valid pointer */
505     WORD   dwMessageId,
506     WORD   dwLanguageId,
507     LPSTR  lpBuffer,     /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
508     WORD   nSize,
509     LPDWORD args         /* [in] NOTE: va_list *args */
510 ) {
511 #ifdef __i386__
512 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
513     LPSTR       target,t;
514     DWORD       talloced;
515     LPSTR       from,f;
516     DWORD       width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
517     BOOL        eos = FALSE;
518     LPSTR       allocstring = NULL;
519
520     TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
521           dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
522         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
523                 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
524         if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
525                 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
526                         || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
527
528     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK) 
529         FIXME("line wrapping (%lu) not supported.\n", width);
530     from = NULL;
531     if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
532         from = HEAP_strdupA( GetProcessHeap(), 0, MapSL(lpSource));
533     if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
534         from = HeapAlloc( GetProcessHeap(),0,200 );
535         sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
536     }
537     if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
538         INT16   bufsize;
539         HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
540
541         dwMessageId &= 0xFFFF;
542         bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
543         if (bufsize) {
544             from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
545             LoadString16(hinst16,dwMessageId,from,bufsize+1);
546         }
547     }
548     target      = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
549     t   = target;
550     talloced= 100;
551
552 #define ADD_TO_T(c) \
553         *t++=c;\
554         if (t-target == talloced) {\
555                 target  = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
556                 t       = target+talloced;\
557                 talloced*=2;\
558         }
559
560     if (from) {
561         f=from;
562         while (*f && !eos) {
563             if (*f=='%') {
564                 int     insertnr;
565                 char    *fmtstr,*x,*lastf;
566                 DWORD   *argliststart;
567
568                 fmtstr = NULL;
569                 lastf = f;
570                 f++;
571                 if (!*f) {
572                     ADD_TO_T('%');
573                     continue;
574                 }
575                 switch (*f) {
576                 case '1':case '2':case '3':case '4':case '5':
577                 case '6':case '7':case '8':case '9':
578                     insertnr=*f-'0';
579                     switch (f[1]) {
580                     case '0':case '1':case '2':case '3':
581                     case '4':case '5':case '6':case '7':
582                     case '8':case '9':
583                         f++;
584                         insertnr=insertnr*10+*f-'0';
585                         f++;
586                         break;
587                     default:
588                         f++;
589                         break;
590                     }
591                     if (*f=='!') {
592                         f++;
593                         if (NULL!=(x=strchr(f,'!'))) {
594                             *x='\0';
595                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
596                             sprintf(fmtstr,"%%%s",f);
597                             f=x+1;
598                         } else {
599                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
600                             sprintf(fmtstr,"%%%s",f);
601                             f+=strlen(f); /*at \0*/
602                         }
603                     } else
604                         if(!args) 
605                             break;
606                         else
607                           fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
608                     if (args) {
609                         int     sz;
610                         LPSTR   b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
611                         
612                         argliststart=args+insertnr-1;
613                        
614                         /* CMF - This makes a BIG assumption about va_list */
615                         while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
616                             b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
617                         }
618                         for (x=b; *x; x++) ADD_TO_T(*x);
619                     } else {
620                         /* NULL args - copy formatstr 
621                          * (probably wrong)
622                          */
623                         while ((lastf<f)&&(*lastf)) {
624                             ADD_TO_T(*lastf++);
625                         }
626                     }
627                     HeapFree(GetProcessHeap(),0,fmtstr);
628                     break;
629                 case '0': /* Just stop processing format string */
630                     eos = TRUE;
631                     f++;
632                     break;
633                 case 'n': /* 16 bit version just outputs 'n' */
634                 default:
635                     ADD_TO_T(*f++);
636                     break;
637                 }
638             } else { /* '\n' or '\r' gets mapped to "\r\n" */
639                 if(*f == '\n' || *f == '\r') {
640                     if (width == 0) {
641                         ADD_TO_T('\r');
642                         ADD_TO_T('\n');
643                         if(*f++ == '\r' && *f == '\n')
644                             f++;
645                     }
646                 } else {
647                     ADD_TO_T(*f++);
648                 }
649             }
650         }
651         *t='\0';
652     }
653     talloced = strlen(target)+1;
654     if (nSize && talloced<nSize) {
655         target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
656     }
657     TRACE("-- %s\n",debugstr_a(target));
658     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
659         /* nSize is the MINIMUM size */
660         HLOCAL16 h = LocalAlloc16(LPTR,talloced);
661         SEGPTR ptr = LocalLock16(h);
662         allocstring = MapSL( ptr );
663         memcpy( allocstring,target,talloced);
664         LocalUnlock16( h );
665         *((HLOCAL16*)lpBuffer) = h;
666     } else
667         lstrcpynA(lpBuffer,target,nSize);
668     HeapFree(GetProcessHeap(),0,target);
669     if (from) HeapFree(GetProcessHeap(),0,from);
670     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? 
671         strlen(allocstring):
672         strlen(lpBuffer);
673 #else
674         return 0;
675 #endif /* __i386__ */
676 }
677 #undef ADD_TO_T