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