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