2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
5 * Copyright 2009 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #define WIN32_NO_STATUS
36 #include "wine/unicode.h"
37 #include "kernel_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
49 /* Messages used by FormatMessage
51 * They can be specified either directly or using a message ID and
52 * loading them from the resource.
54 * The resourcedata has following format:
56 * 0: DWORD nrofentries
57 * nrofentries * subentry:
60 * 8: DWORD offset from start to the stringentries
62 * (lastentry-firstentry) * stringentry:
63 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
66 * (stringentry i of a subentry refers to the ID 'firstentry+i')
68 * Yes, ANSI strings in win32 resources. Go figure.
71 static const WCHAR PCNTFMTWSTR
[] = { '%','%','%','s',0 };
72 static const WCHAR FMTWSTR
[] = { '%','s',0 };
74 /**********************************************************************
75 * load_messageW (internal)
77 static LPWSTR
load_messageW( HMODULE module
, UINT id
, WORD lang
)
79 const MESSAGE_RESOURCE_ENTRY
*mre
;
83 TRACE("module = %p, id = %08x\n", module
, id
);
85 if (!module
) module
= GetModuleHandleW( NULL
);
86 if ((status
= RtlFindMessage( module
, RT_MESSAGETABLE
, lang
, id
, &mre
)) != STATUS_SUCCESS
)
88 SetLastError( RtlNtStatusToDosError(status
) );
92 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
94 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
95 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
96 memcpy( buffer
, mre
->Text
, len
);
100 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
101 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
102 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
104 TRACE("returning %s\n", wine_dbgstr_w(buffer
));
109 /**********************************************************************
110 * load_messageA (internal)
112 static LPSTR
load_messageA( HMODULE module
, UINT id
, WORD lang
)
114 const MESSAGE_RESOURCE_ENTRY
*mre
;
118 TRACE("module = %p, id = %08x\n", module
, id
);
120 if (!module
) module
= GetModuleHandleW( NULL
);
121 if ((status
= RtlFindMessage( module
, RT_MESSAGETABLE
, lang
, id
, &mre
)) != STATUS_SUCCESS
)
123 SetLastError( RtlNtStatusToDosError(status
) );
127 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
129 int len
= WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, NULL
, 0, NULL
, NULL
);
130 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
131 WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, buffer
, len
, NULL
, NULL
);
135 int len
= strlen((const char*)mre
->Text
) + 1;
136 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
137 memcpy( buffer
, mre
->Text
, len
);
139 TRACE("returning %s\n", wine_dbgstr_a(buffer
));
144 /**********************************************************************
147 static ULONG_PTR
get_arg( int nr
, DWORD flags
, struct format_args
*args
)
149 if (nr
== -1) nr
= args
->last
+ 1;
152 if (!args
->args
) args
->args
= HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR
) );
153 while (nr
> args
->last
)
154 args
->args
[args
->last
++] = va_arg( *args
->list
, ULONG_PTR
);
156 if (nr
> args
->last
) args
->last
= nr
;
157 return args
->args
[nr
- 1];
161 /**********************************************************************
162 * format_insertA (internal)
164 static LPCSTR
format_insertA( int insert
, LPCSTR format
, DWORD flags
,
165 struct format_args
*args
, LPSTR
*result
)
167 char *astring
= NULL
, *p
, fmt
[256];
171 if (*format
!= '!') /* simple string */
173 char *str
= (char *)get_arg( insert
, flags
, args
);
174 *result
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
175 strcpy( *result
, str
);
183 while (*format
== '0' ||
192 p
+= sprintf( p
, "%lu", get_arg( insert
, flags
, args
));
196 else *p
++ = *format
++;
198 while (isdigit(*format
)) *p
++ = *format
++;
205 p
+= sprintf( p
, "%lu", get_arg( insert
, flags
, args
));
210 while (isdigit(*format
)) *p
++ = *format
++;
213 /* replicate MS bug: drop an argument when using va_list with width/precision */
214 if (insert
== -1 && args
->list
) args
->last
--;
215 arg
= get_arg( insert
, flags
, args
);
217 /* check for wide string format */
218 if ((format
[0] == 'l' && format
[1] == 's') ||
219 (format
[0] == 'l' && format
[1] == 'S') ||
220 (format
[0] == 'w' && format
[1] == 's') ||
223 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, (WCHAR
*)arg
, -1, /*FIXME*/
224 NULL
, 0, NULL
, NULL
);
225 astring
= HeapAlloc( GetProcessHeap(), 0, len
);
226 WideCharToMultiByte( CP_ACP
, 0, (WCHAR
*)arg
, -1, astring
, len
, NULL
, NULL
);
227 arg
= (ULONG_PTR
)astring
;
230 /* check for wide character format */
231 else if ((format
[0] == 'l' && format
[1] == 'c') ||
232 (format
[0] == 'l' && format
[1] == 'C') ||
233 (format
[0] == 'w' && format
[1] == 'c') ||
237 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, NULL
, 0, NULL
, NULL
);
238 astring
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 );
239 WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, astring
, len
, NULL
, NULL
);
241 arg
= (ULONG_PTR
)astring
;
244 /* check for ascii string format */
245 else if ((format
[0] == 'h' && format
[1] == 's') ||
246 (format
[0] == 'h' && format
[1] == 'S'))
250 /* check for ascii character format */
251 else if ((format
[0] == 'h' && format
[1] == 'c') ||
252 (format
[0] == 'h' && format
[1] == 'C'))
256 /* FIXME: handle I64 etc. */
257 else while (*format
&& *format
!= '!') *p
++ = *format
++;
263 char *ret
= HeapAlloc( GetProcessHeap(), 0, size
);
264 int needed
= snprintf( ret
, size
, fmt
, arg
);
265 if (needed
== -1 || needed
>= size
)
267 HeapFree( GetProcessHeap(), 0, ret
);
268 size
= max( needed
+ 1, size
* 2 );
277 while (*format
&& *format
!= '!') format
++;
278 if (*format
== '!') format
++;
280 HeapFree( GetProcessHeap(), 0, astring
);
285 /**********************************************************************
286 * format_insertW (internal)
288 static LPCWSTR
format_insertW( int insert
, LPCWSTR format
, DWORD flags
,
289 struct format_args
*args
, LPWSTR
*result
)
291 static const WCHAR fmt_lu
[] = {'%','l','u',0};
292 WCHAR
*wstring
= NULL
, *p
, fmt
[256];
296 if (*format
!= '!') /* simple string */
298 WCHAR
*str
= (WCHAR
*)get_arg( insert
, flags
, args
);
299 *result
= HeapAlloc( GetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(WCHAR
) );
300 strcpyW( *result
, str
);
308 while (*format
== '0' ||
317 p
+= sprintfW( p
, fmt_lu
, get_arg( insert
, flags
, args
));
321 else *p
++ = *format
++;
323 while (isdigitW(*format
)) *p
++ = *format
++;
330 p
+= sprintfW( p
, fmt_lu
, get_arg( insert
, flags
, args
));
335 while (isdigitW(*format
)) *p
++ = *format
++;
338 /* replicate MS bug: drop an argument when using va_list with width/precision */
339 if (insert
== -1 && args
->list
) args
->last
--;
340 arg
= get_arg( insert
, flags
, args
);
342 /* check for ascii string format */
343 if ((format
[0] == 'h' && format
[1] == 's') ||
344 (format
[0] == 'h' && format
[1] == 'S') ||
347 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, /*FIXME*/ NULL
, 0 );
348 wstring
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
349 MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, wstring
, len
);
350 arg
= (ULONG_PTR
)wstring
;
353 /* check for ascii character format */
354 else if ((format
[0] == 'h' && format
[1] == 'c') ||
355 (format
[0] == 'h' && format
[1] == 'C') ||
359 wstring
= HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR
) );
360 MultiByteToWideChar( CP_ACP
, 0, &ch
, 1, wstring
, 1 );
362 arg
= (ULONG_PTR
)wstring
;
365 /* check for wide string format */
366 else if ((format
[0] == 'l' && format
[1] == 's') ||
367 (format
[0] == 'l' && format
[1] == 'S') ||
368 (format
[0] == 'w' && format
[1] == 's'))
372 /* check for wide character format */
373 else if ((format
[0] == 'l' && format
[1] == 'c') ||
374 (format
[0] == 'l' && format
[1] == 'C') ||
375 (format
[0] == 'w' && format
[1] == 'c'))
379 /* FIXME: handle I64 etc. */
380 else while (*format
&& *format
!= '!') *p
++ = *format
++;
386 WCHAR
*ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
387 int needed
= snprintfW( ret
, size
, fmt
, arg
);
388 if (needed
== -1 || needed
>= size
)
390 HeapFree( GetProcessHeap(), 0, ret
);
391 size
= max( needed
+ 1, size
* 2 );
400 while (*format
&& *format
!= '!') format
++;
401 if (*format
== '!') format
++;
403 HeapFree( GetProcessHeap(), 0, wstring
);
408 /***********************************************************************
409 * FormatMessageA (KERNEL32.@)
410 * FIXME: missing wrap,
412 DWORD WINAPI
FormatMessageA(
421 struct format_args format_args
;
427 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
431 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
432 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
433 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
434 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
435 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
439 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
443 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
445 format_args
.args
= (ULONG_PTR
*)args
;
446 format_args
.list
= NULL
;
447 format_args
.last
= 0;
451 format_args
.args
= NULL
;
452 format_args
.list
= args
;
453 format_args
.last
= 0;
456 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
457 FIXME("line wrapping (%u) not supported.\n", width
);
459 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
461 from
= HeapAlloc( GetProcessHeap(), 0, strlen(lpSource
) + 1 );
462 strcpy( from
, lpSource
);
466 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
467 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
468 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
469 from
= load_messageA( kernel32_handle
, dwMessageId
, dwLanguageId
);
472 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
476 #define ADD_TO_T(c) do { \
478 if ((DWORD)(t-target) == talloced) {\
479 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
480 t = target+talloced;\
487 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
503 case '1':case '2':case '3':case '4':case '5':
504 case '6':case '7':case '8':case '9':
507 case '0':case '1':case '2':case '3':
508 case '4':case '5':case '6':case '7':
511 insertnr
=insertnr
*10+*f
-'0';
518 f
= format_insertA( insertnr
, f
, dwFlags
, &format_args
, &str
);
519 for (x
= str
; *x
; x
++) ADD_TO_T(*x
);
520 HeapFree( GetProcessHeap(), 0, str
);
567 talloced
= strlen(target
)+1;
568 if (nSize
&& talloced
<nSize
) {
569 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
571 TRACE("-- %s\n",debugstr_a(target
));
572 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
573 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
574 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
576 lstrcpynA(lpBuffer
,target
,nSize
);
578 HeapFree(GetProcessHeap(),0,target
);
579 HeapFree(GetProcessHeap(),0,from
);
580 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
581 ret
= (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
) : strlen(lpBuffer
);
582 TRACE("-- returning %d\n", ret
);
588 /***********************************************************************
589 * FormatMessageW (KERNEL32.@)
591 DWORD WINAPI
FormatMessageW(
600 struct format_args format_args
;
605 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
609 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
610 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
611 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
612 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
613 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
617 SetLastError(ERROR_INVALID_PARAMETER
);
621 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
623 format_args
.args
= (ULONG_PTR
*)args
;
624 format_args
.list
= NULL
;
625 format_args
.last
= 0;
629 format_args
.args
= NULL
;
630 format_args
.list
= args
;
631 format_args
.last
= 0;
634 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
635 FIXME("line wrapping not supported.\n");
637 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
638 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource
) + 1) *
640 strcpyW( from
, lpSource
);
644 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
645 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
646 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
647 from
= load_messageW( kernel32_handle
, dwMessageId
, dwLanguageId
);
650 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
654 #define ADD_TO_T(c) do {\
656 if ((DWORD)(t-target) == talloced) {\
657 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
658 t = target+talloced;\
665 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
682 case '1':case '2':case '3':case '4':case '5':
683 case '6':case '7':case '8':case '9':
686 case '0':case '1':case '2':case '3':
687 case '4':case '5':case '6':case '7':
690 insertnr
=insertnr
*10+*f
-'0';
697 f
= format_insertW( insertnr
, f
, dwFlags
, &format_args
, &str
);
698 for (x
= str
; *x
; x
++) ADD_TO_T(*x
);
699 HeapFree( GetProcessHeap(), 0, str
);
746 talloced
= strlenW(target
)+1;
747 if (nSize
&& talloced
<nSize
)
748 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
749 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
750 /* nSize is the MINIMUM size */
751 DWORD len
= strlenW(target
) + 1;
752 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
753 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
755 else lstrcpynW(lpBuffer
, target
, nSize
);
757 HeapFree(GetProcessHeap(),0,target
);
758 HeapFree(GetProcessHeap(),0,from
);
759 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
760 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
761 *(LPWSTR
*)lpBuffer
: lpBuffer
));
762 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
763 strlenW(*(LPWSTR
*)lpBuffer
):