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