Consequential changes to default system font sizes
[wine] / misc / lstr.c
1 /*
2  * String functions
3  *
4  * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5  * Copyright 1996 Marcus Meissner
6  */
7
8 #include "config.h"
9
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 #ifdef HAVE_WCTYPE_H
16 # include <wctype.h>
17 #else
18 # define iswalnum(c) isalnum(c)
19 # define iswalpha(c) isalpha(c)
20 # define iswupper(c) isupper(c)
21 # define iswlower(c) islower(c)
22 #endif  /* HAVE_WCTYPE_H */
23
24 #include "wine/winbase16.h"
25 #include "wine/winuser16.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "task.h"
30 #include "heap.h"
31 #include "ldt.h"
32 #include "stackframe.h"
33 #include "module.h"
34 #include "debugtools.h"
35
36 DEFAULT_DEBUG_CHANNEL(resource)
37
38 extern const WORD OLE2NLS_CT_CType3_LUT[]; /* FIXME: does not belong here */
39
40
41 /* Funny to divide them between user and kernel. */
42
43 /* be careful: always use functions from wctype.h if character > 255 */
44
45 /*
46  * Unicode case conversion routines ... these should be used where
47  * toupper/tolower are used for ASCII.
48  */
49 #ifndef HAVE_WCTYPE_H
50 /* FIXME: should probably get rid of wctype.h altogether */
51 #include "casemap.h"
52
53 WCHAR towupper(WCHAR code)
54 {
55     const WCHAR * ptr = uprtable[HIBYTE(code)];
56     return ptr ? ptr[LOBYTE(code)] : code;
57 }
58
59 WCHAR towlower(WCHAR code)
60 {
61     const WCHAR * ptr = lwrtable[HIBYTE(code)];
62     return ptr ? ptr[LOBYTE(code)] : code;
63 }
64 #endif  /* HAVE_WCTYPE_H */
65
66 /***********************************************************************
67  *              IsCharAlpha (USER.433)
68  */
69 BOOL16 WINAPI IsCharAlpha16(CHAR ch)
70 {
71   return isalpha(ch);   /* This is probably not right for NLS */
72 }
73
74 /***********************************************************************
75  *              IsCharAlphanumeric (USER.434)
76  */
77 BOOL16 WINAPI IsCharAlphaNumeric16(CHAR ch)
78 {
79     return isalnum(ch);
80 }
81
82 /***********************************************************************
83  *              IsCharUpper (USER.435)
84  */
85 BOOL16 WINAPI IsCharUpper16(CHAR ch)
86 {
87   return isupper(ch);
88 }
89
90 /***********************************************************************
91  *              IsCharLower (USER.436)
92  */
93 BOOL16 WINAPI IsCharLower16(CHAR ch)
94 {
95   return islower(ch);
96 }
97
98 /***********************************************************************
99  *           AnsiUpper16   (USER.431)
100  */
101 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
102 {
103   /* I am not sure if the locale stuff works with toupper, but then again 
104      I am not sure if the Linux libc locale stuffs works at all */
105
106     /* uppercase only one char if strOrChar < 0x10000 */
107     if (HIWORD(strOrChar))
108     {
109         char *s;
110         for (s = PTR_SEG_TO_LIN(strOrChar); *s; s++) *s = toupper(*s);
111         return strOrChar;
112     }
113     else return toupper((char)strOrChar);
114 }
115
116
117 /***********************************************************************
118  *           AnsiUpperBuff16   (USER.437)
119  */
120 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
121 {
122     UINT count = len ? len : 65536;
123     for (; count; count--, str++) *str = toupper(*str);
124     return len;
125 }
126
127 /***********************************************************************
128  *           AnsiLower16   (USER.432)
129  */
130 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
131 {
132   /* I am not sure if the locale stuff works with toupper, but then again 
133      I am not sure if the Linux libc locale stuffs works at all */
134
135     /* lowercase only one char if strOrChar < 0x10000 */
136     if (HIWORD(strOrChar))
137     {
138         char *s;
139         for (s = PTR_SEG_TO_LIN( strOrChar ); *s; s++) *s = tolower( *s );
140         return strOrChar;
141     }
142     else return tolower((char)strOrChar);
143 }
144
145
146 /***********************************************************************
147  *           AnsiLowerBuff16   (USER.438)
148  */
149 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
150 {
151     UINT count = len ? len : 65536;
152     for (; count; count--, str++) *str = tolower(*str);
153     return len;
154 }
155
156
157 /***********************************************************************
158  *           AnsiNext16   (USER.472)
159  */
160 SEGPTR WINAPI AnsiNext16(SEGPTR current)
161 {
162     return (*(char *)PTR_SEG_TO_LIN(current)) ? current + 1 : current;
163 }
164
165
166 /***********************************************************************
167  *           AnsiPrev16   (USER.473)
168  */
169 SEGPTR WINAPI AnsiPrev16( SEGPTR start, SEGPTR current )
170 {
171     return (current == start) ? start : current - 1;
172 }
173
174
175 /***********************************************************************
176  *           CharNext32A   (USER32.29)
177  */
178 LPSTR WINAPI CharNextA( LPCSTR ptr )
179 {
180     if (!*ptr) return (LPSTR)ptr;
181     if (IsDBCSLeadByte( *ptr )) return (LPSTR)(ptr + 2);
182     return (LPSTR)(ptr + 1);
183 }
184
185
186 /***********************************************************************
187  *           CharNextEx32A   (USER32.30)
188  */
189 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
190 {
191     if (!*ptr) return (LPSTR)ptr;
192     if (IsDBCSLeadByteEx( codepage, *ptr )) return (LPSTR)(ptr + 2);
193     return (LPSTR)(ptr + 1);
194 }
195
196
197 /***********************************************************************
198  *           CharNextExW   (USER32.31)
199  */
200 LPWSTR WINAPI CharNextExW(WORD codepage,LPCWSTR x,DWORD flags)
201 {
202     /* FIXME: add DBCS / codepage stuff */
203     if (*x) return (LPWSTR)(x+1);
204     else return (LPWSTR)x;
205 }
206
207 /***********************************************************************
208  *           CharNextW   (USER32.32)
209  */
210 LPWSTR WINAPI CharNextW(LPCWSTR x)
211 {
212     if (*x) return (LPWSTR)(x+1);
213     else return (LPWSTR)x;
214 }
215
216 /***********************************************************************
217  *           CharPrev32A   (USER32.33)
218  */
219 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
220 {
221     while (*start && (start < ptr))
222     {
223         LPCSTR next = CharNextA( start );
224         if (next >= ptr) break;
225         start = next;
226     }
227     return (LPSTR)start;
228 }
229
230
231 /***********************************************************************
232  *           CharPrevEx32A   (USER32.34)
233  */
234 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
235 {
236     while (*start && (start < ptr))
237     {
238         LPCSTR next = CharNextExA( codepage, start, flags );
239         if (next > ptr) break;
240         start = next;
241     }
242     return (LPSTR)start;
243 }
244
245
246 /***********************************************************************
247  *           CharPrevExW   (USER32.35)
248  */
249 LPWSTR WINAPI CharPrevExW(WORD codepage,LPCWSTR start,LPCWSTR x,DWORD flags)
250 {
251     /* FIXME: add DBCS / codepage stuff */
252     if (x>start) return (LPWSTR)(x-1);
253     else return (LPWSTR)x;
254 }
255
256 /***********************************************************************
257  *           CharPrevW   (USER32.36)
258  */
259 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
260 {
261     if (x>start) return (LPWSTR)(x-1);
262     else return (LPWSTR)x;
263 }
264
265 /***********************************************************************
266  *           CharLowerA   (USER32.25)
267  * FIXME: handle current locale
268  */
269 LPSTR WINAPI CharLowerA(LPSTR x)
270 {
271     LPSTR       s;
272
273     if (HIWORD(x))
274     {
275         s=x;
276         while (*s)
277         {
278             *s=tolower(*s);
279             s++;
280         }
281         return x;
282     }
283     else return (LPSTR)tolower((char)(int)x);
284 }
285
286 /***********************************************************************
287  *           CharLowerBuffA   (USER32.26)
288  * FIXME: handle current locale
289  */
290 DWORD WINAPI CharLowerBuffA(LPSTR x,DWORD buflen)
291 {
292     DWORD done=0;
293
294     if (!x) return 0; /* YES */
295     while (*x && (buflen--))
296     {
297         *x=tolower(*x);
298         x++;
299         done++;
300     }
301     return done;
302 }
303
304 /***********************************************************************
305  *           CharLowerBuffW   (USER32.27)
306  * FIXME: handle current locale
307  */
308 DWORD WINAPI CharLowerBuffW(LPWSTR x,DWORD buflen)
309 {
310     DWORD done=0;
311
312     if (!x) return 0; /* YES */
313     while (*x && (buflen--))
314     {
315         *x=towlower(*x);
316         x++;
317         done++;
318     }
319     return done;
320 }
321
322 /***********************************************************************
323  *           CharLowerW   (USER32.28)
324  * FIXME: handle current locale
325  */
326 LPWSTR WINAPI CharLowerW(LPWSTR x)
327 {
328     if (HIWORD(x))
329     {
330         LPWSTR s = x;
331         while (*s)
332         {
333             *s=towlower(*s);
334             s++;
335         }
336         return x;
337     }
338     else return (LPWSTR)((UINT)towlower(LOWORD(x)));
339 }
340
341 /***********************************************************************
342  *           CharUpper32A   (USER32.41)
343  * FIXME: handle current locale
344  */
345 LPSTR WINAPI CharUpperA(LPSTR x)
346 {
347     if (HIWORD(x))
348     {
349         LPSTR s = x;
350         while (*s)
351         {
352             *s=toupper(*s);
353             s++;
354         }
355         return x;
356     }
357     return (LPSTR)toupper((char)(int)x);
358 }
359
360 /***********************************************************************
361  *           CharUpperBuffA   (USER32.42)
362  * FIXME: handle current locale
363  */
364 DWORD WINAPI CharUpperBuffA(LPSTR x,DWORD buflen)
365 {
366     DWORD done=0;
367
368     if (!x) return 0; /* YES */
369     while (*x && (buflen--))
370     {
371         *x=toupper(*x);
372         x++;
373         done++;
374     }
375     return done;
376 }
377
378 /***********************************************************************
379  *           CharUpperBuffW   (USER32.43)
380  * FIXME: handle current locale
381  */
382 DWORD WINAPI CharUpperBuffW(LPWSTR x,DWORD buflen)
383 {
384     DWORD done=0;
385
386     if (!x) return 0; /* YES */
387     while (*x && (buflen--))
388     {
389         *x=towupper(*x);
390         x++;
391         done++;
392     }
393     return done;
394 }
395
396 /***********************************************************************
397  *           CharUpperW   (USER32.44)
398  * FIXME: handle current locale
399  */
400 LPWSTR WINAPI CharUpperW(LPWSTR x)
401 {
402     if (HIWORD(x))
403     {
404         LPWSTR s = x;
405         while (*s)
406         {
407             *s=towupper(*s);
408             s++;
409         }
410         return x;
411     }
412     else return (LPWSTR)((UINT)towupper(LOWORD(x)));
413 }
414
415 /***********************************************************************
416  *           IsCharAlphaA   (USER32.331)
417  * FIXME: handle current locale
418  */
419 BOOL WINAPI IsCharAlphaA(CHAR x)
420 {
421     return (OLE2NLS_CT_CType3_LUT[(unsigned char)x] & C3_ALPHA);
422 }
423
424 /***********************************************************************
425  *           IsCharAlphaNumericA   (USER32.332)
426  * FIXME: handle current locale
427  */
428 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
429 {
430     return IsCharAlphaA(x) || isdigit(x) ;
431 }
432
433 /***********************************************************************
434  *           IsCharAlphaNumericW   (USER32.333)
435  * FIXME: handle current locale
436  */
437 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
438 {
439     return iswalnum(x);
440 }
441
442 /***********************************************************************
443  *           IsCharAlphaW   (USER32.334)
444  * FIXME: handle current locale
445  */
446 BOOL WINAPI IsCharAlphaW(WCHAR x)
447 {
448     return iswalpha(x);
449 }
450
451 /***********************************************************************
452  *           IsCharLower32A   (USER32.335)
453  * FIXME: handle current locale
454  */
455 BOOL WINAPI IsCharLowerA(CHAR x)
456 {
457     return islower(x);
458 }
459
460 /***********************************************************************
461  *           IsCharLower32W   (USER32.336)
462  * FIXME: handle current locale
463  */
464 BOOL WINAPI IsCharLowerW(WCHAR x)
465 {
466     return iswlower(x);
467 }
468
469 /***********************************************************************
470  *           IsCharUpper32A   (USER32.337)
471  * FIXME: handle current locale
472  */
473 BOOL WINAPI IsCharUpperA(CHAR x)
474 {
475     return isupper(x);
476 }
477
478 /***********************************************************************
479  *           IsCharUpper32W   (USER32.338)
480  * FIXME: handle current locale
481  */
482 BOOL WINAPI IsCharUpperW(WCHAR x)
483 {
484     return iswupper(x);
485 }
486
487 /***********************************************************************
488  *           FormatMessage16   (USER.606)
489  */
490 DWORD WINAPI FormatMessage16(
491     DWORD   dwFlags,
492     LPCVOID lpSource,
493     WORD   dwMessageId,
494     WORD   dwLanguageId,
495     LPSTR   lpBuffer,
496     WORD   nSize,
497     LPDWORD args /* va_list *args */
498 ) {
499     return FormatMessageA(dwFlags, lpSource, (DWORD)dwMessageId, (DWORD)dwLanguageId, lpBuffer, (DWORD)nSize, args);
500 }
501
502 /***********************************************************************
503  *           FormatMessage32A   (KERNEL32.138)
504  * FIXME: missing wrap,FROM_SYSTEM message-loading,
505  */
506 DWORD WINAPI FormatMessageA(
507         DWORD   dwFlags,
508         LPCVOID lpSource,
509         DWORD   dwMessageId,
510         DWORD   dwLanguageId,
511         LPSTR   lpBuffer,
512         DWORD   nSize,
513         LPDWORD args /* va_list *args */
514 ) {
515 #ifdef __i386__
516 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
517         LPSTR   target,t;
518         DWORD   talloced;
519         LPSTR   from,f;
520         DWORD   width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
521         DWORD   nolinefeed = 0;
522
523         TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
524                      dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
525         if (width) 
526                 FIXME("line wrapping not supported.\n");
527         from = NULL;
528         if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
529                 from = HEAP_strdupA( GetProcessHeap(), 0, (LPSTR)lpSource);
530         if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
531                 from = HeapAlloc( GetProcessHeap(),0,200 );
532                 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
533         }
534         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
535                 INT     bufsize;
536
537                 dwMessageId &= 0xFFFF;
538                 bufsize=LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,NULL,100);
539                 if (bufsize) {
540                         from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
541                         LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,from,bufsize+1);
542                 }
543         }
544         target  = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
545         t       = target;
546         talloced= 100;
547
548 #define ADD_TO_T(c) \
549         *t++=c;\
550         if (t-target == talloced) {\
551                 target  = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
552                 t       = target+talloced;\
553                 talloced*=2;\
554         }
555
556         if (from) {
557                 f=from;
558                 while (*f && !nolinefeed) {
559                         if (*f=='%') {
560                                 int     insertnr;
561                                 char    *fmtstr,*sprintfbuf,*x,*lastf;
562                                 DWORD   *argliststart;
563
564                                 fmtstr = NULL;
565                                 lastf = f;
566                                 f++;
567                                 if (!*f) {
568                                         ADD_TO_T('%');
569                                         continue;
570                                 }
571                                 switch (*f) {
572                                 case '1':case '2':case '3':case '4':case '5':
573                                 case '6':case '7':case '8':case '9':
574                                         insertnr=*f-'0';
575                                         switch (f[1]) {
576                                         case '0':case '1':case '2':case '3':
577                                         case '4':case '5':case '6':case '7':
578                                         case '8':case '9':
579                                                 f++;
580                                                 insertnr=insertnr*10+*f-'0';
581                                                 f++;
582                                                 break;
583                                         default:
584                                                 f++;
585                                                 break;
586                                         }
587                                         if (*f=='!') {
588                                                 f++;
589                                                 if (NULL!=(x=strchr(f,'!'))) {
590                                                         *x='\0';
591                                                         fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
592                                                         sprintf(fmtstr,"%%%s",f);
593                                                         f=x+1;
594                                                 } else {
595                                                         fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
596                                                         sprintf(fmtstr,"%%%s",f);
597                                                         f+=strlen(f); /*at \0*/
598                                                 }
599                                         } else
600                                                 if(!args) 
601                                                   break;
602                                         else
603                                                 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
604                                         if (args) {
605                                                 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
606                                                         argliststart=args+insertnr-1;
607                                                 else
608                                                     argliststart=(*(DWORD**)args)+insertnr-1;
609
610                                                 if (fmtstr[strlen(fmtstr)-1]=='s')
611                                                         sprintfbuf=HeapAlloc(GetProcessHeap(),0,strlen((LPSTR)argliststart[0])+1);
612                                                 else
613                                                         sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
614
615                                                 /* CMF - This makes a BIG assumption about va_list */
616                                                 wvsprintfA(sprintfbuf, fmtstr, (va_list) argliststart);
617                                                 x=sprintfbuf;
618                                                 while (*x) {
619                                                         ADD_TO_T(*x++);
620                                                 }
621                                                 HeapFree(GetProcessHeap(),0,sprintfbuf);
622                                         } else {
623                                                 /* NULL args - copy formatstr 
624                                                  * (probably wrong)
625                                                  */
626                                                 while ((lastf<f)&&(*lastf)) {
627                                                         ADD_TO_T(*lastf++);
628                                                 }
629                                         }
630                                         HeapFree(GetProcessHeap(),0,fmtstr);
631                                         break;
632                                 case 'n':
633                                         /* FIXME: perhaps add \r too? */
634                                         ADD_TO_T('\n');
635                                         f++;
636                                         break;
637                                 case '0':
638                                         nolinefeed=1;
639                                         f++;
640                                         break;
641                                 default:ADD_TO_T(*f++)
642                                         break;
643
644                                 }
645                         } else {
646                                 ADD_TO_T(*f++)
647                         }
648                 }
649                 *t='\0';
650         }
651         if (!nolinefeed) {
652             /* add linefeed */
653             if(t==target || t[-1]!='\n')
654                 ADD_TO_T('\n'); /* FIXME: perhaps add \r too? */
655         }
656         talloced = strlen(target)+1;
657         if (nSize && talloced<nSize) {
658                 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
659         }
660     TRACE("-- %s\n",debugstr_a(target));
661         if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
662                 /* nSize is the MINIMUM size */
663                 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,talloced);
664                 memcpy(*(LPSTR*)lpBuffer,target,talloced);
665         } else
666                 strncpy(lpBuffer,target,nSize);
667         HeapFree(GetProcessHeap(),0,target);
668         if (from) HeapFree(GetProcessHeap(),0,from);
669         return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? 
670                         strlen(*(LPSTR*)lpBuffer):
671                         strlen(lpBuffer);
672 #else
673         return 0;
674 #endif /* __i386__ */
675 }
676 #undef ADD_TO_T
677
678
679 /***********************************************************************
680  *           FormatMessage32W   (KERNEL32.138)
681  */
682 DWORD WINAPI FormatMessageW(
683         DWORD   dwFlags,
684         LPCVOID lpSource,
685         DWORD   dwMessageId,
686         DWORD   dwLanguageId,
687         LPWSTR  lpBuffer,
688         DWORD   nSize,
689         LPDWORD args /* va_list *args */
690 ) {
691 #ifdef __i386__
692 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
693         LPSTR   target,t;
694         DWORD   talloced;
695         LPSTR   from,f;
696         DWORD   width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
697         DWORD   nolinefeed = 0;
698
699         TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
700                      dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
701         if (width) 
702                 FIXME("line wrapping not supported.\n");
703         from = NULL;
704         if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
705                 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
706         if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
707                 /* gather information from system message tables ... */
708                 from = HeapAlloc( GetProcessHeap(),0,200 );
709                 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
710         }
711         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
712                 INT     bufsize;
713
714                 dwMessageId &= 0xFFFF;
715                 bufsize=LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,NULL,100);
716                 if (bufsize)
717                 {
718                     from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
719                     LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,from,bufsize+1);
720                 }
721         }
722         target  = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
723         t       = target;
724         talloced= 100;
725
726 #define ADD_TO_T(c) \
727         *t++=c;\
728         if (t-target == talloced) {\
729                 target  = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
730                 t       = target+talloced;\
731                 talloced*=2;\
732         }
733
734         if (from) {
735                 f=from;
736                 while (*f && !nolinefeed) {
737                         if (*f=='%') {
738                                 int     insertnr;
739                                 char    *fmtstr,*sprintfbuf,*x;
740                                 DWORD   *argliststart;
741
742                                 fmtstr = NULL;
743                                 f++;
744                                 if (!*f) {
745                                         ADD_TO_T('%');
746                                         continue;
747                                 }
748                                 switch (*f) {
749                                 case '1':case '2':case '3':case '4':case '5':
750                                 case '6':case '7':case '8':case '9':
751                                         insertnr=*f-'0';
752                                         switch (f[1]) {
753                                         case '0':case '1':case '2':case '3':
754                                         case '4':case '5':case '6':case '7':
755                                         case '8':case '9':
756                                                 f++;
757                                                 insertnr=insertnr*10+*f-'0';
758                                                 f++;
759                                                 break;
760                                         default:
761                                                 f++;
762                                                 break;
763                                         }
764                                         if (*f=='!') {
765                                                 f++;
766                                                 if (NULL!=(x=strchr(f,'!')))
767                                                 {
768                                                     *x='\0';
769                                                     fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
770                                                         sprintf(fmtstr,"%%%s",f);
771                                                         f=x+1;
772                                                 } else {
773                                                         fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
774                                                         sprintf(fmtstr,"%%%s",f);
775                                                         f+=strlen(f); /*at \0*/
776                                                 }
777                                         } else
778                                                 if(!args)
779                                                   break;
780                                         else
781                                                 fmtstr=HEAP_strdupA( GetProcessHeap(),0,"%s");
782                                         if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
783                                                 argliststart=args+insertnr-1;
784                                         else
785                                                 argliststart=(*(DWORD**)args)+insertnr-1;
786
787                                         if (fmtstr[strlen(fmtstr)-1]=='s') {
788                                                 DWORD   xarr[3];
789
790                                                 xarr[0]=(DWORD)HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)(*(argliststart+0)));
791                                                 /* possible invalid pointers */
792                                                 xarr[1]=*(argliststart+1);
793                                                 xarr[2]=*(argliststart+2);
794                                                 sprintfbuf=HeapAlloc(GetProcessHeap(),0,lstrlenW((LPWSTR)argliststart[0])*2+1);
795
796                                                 /* CMF - This makes a BIG assumption about va_list */
797                                                 vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
798                                         } else {
799                                                 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
800
801                                                 /* CMF - This makes a BIG assumption about va_list */
802                                                 wvsprintfA(sprintfbuf, fmtstr, (va_list) argliststart);
803                                         }
804                                         x=sprintfbuf;
805                                         while (*x) {
806                                                 ADD_TO_T(*x++);
807                                         }
808                                         HeapFree(GetProcessHeap(),0,sprintfbuf);
809                                         HeapFree(GetProcessHeap(),0,fmtstr);
810                                         break;
811                                 case 'n':
812                                         /* FIXME: perhaps add \r too? */
813                                         ADD_TO_T('\n');
814                                         f++;
815                                         break;
816                                 case '0':
817                                         nolinefeed=1;
818                                         f++;
819                                         break;
820                                 default:ADD_TO_T(*f++)
821                                         break;
822
823                                 }
824                         } else {
825                                 ADD_TO_T(*f++)
826                         }
827                 }
828                 *t='\0';
829         }
830         if (!nolinefeed) {
831             /* add linefeed */
832             if(t==target || t[-1]!='\n')
833                 ADD_TO_T('\n'); /* FIXME: perhaps add \r too? */
834         }
835         talloced = strlen(target)+1;
836         if (nSize && talloced<nSize)
837                 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
838         if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
839                 /* nSize is the MINIMUM size */
840                 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,talloced*2+2);
841                 lstrcpynAtoW(*(LPWSTR*)lpBuffer,target,talloced);
842         } else
843                 lstrcpynAtoW(lpBuffer,target,nSize);
844         HeapFree(GetProcessHeap(),0,target);
845         if (from) HeapFree(GetProcessHeap(),0,from);
846         return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? 
847                         lstrlenW(*(LPWSTR*)lpBuffer):
848                         lstrlenW(lpBuffer);
849 #else
850         return 0;
851 #endif /* __i386__ */
852 }
853 #undef ADD_TO_T