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 /**********************************************************************
72 * load_message (internal)
74 static LPWSTR
load_message( HMODULE module
, UINT id
, WORD lang
)
76 const MESSAGE_RESOURCE_ENTRY
*mre
;
80 TRACE("module = %p, id = %08x\n", module
, id
);
82 if (!module
) module
= GetModuleHandleW( NULL
);
83 if ((status
= RtlFindMessage( module
, RT_MESSAGETABLE
, lang
, id
, &mre
)) != STATUS_SUCCESS
)
85 SetLastError( RtlNtStatusToDosError(status
) );
89 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
91 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
92 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
93 memcpy( buffer
, mre
->Text
, len
);
97 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
98 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
99 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
101 TRACE("returning %s\n", wine_dbgstr_w(buffer
));
105 static LPWSTR
search_message( DWORD flags
, HMODULE module
, UINT id
, WORD lang
)
108 if (flags
& FORMAT_MESSAGE_FROM_HMODULE
)
109 from
= load_message( module
, id
, lang
);
110 if (!from
&& (flags
& FORMAT_MESSAGE_FROM_SYSTEM
))
112 /* Fold win32 hresult to its embedded error code. */
113 if (HRESULT_SEVERITY(id
) == SEVERITY_ERROR
&&
114 HRESULT_FACILITY(id
) == FACILITY_WIN32
)
116 id
= HRESULT_CODE(id
);
118 from
= load_message( kernel32_handle
, id
, lang
);
123 /**********************************************************************
126 static ULONG_PTR
get_arg( int nr
, DWORD flags
, struct format_args
*args
)
128 if (nr
== -1) nr
= args
->last
+ 1;
131 if (!args
->args
) args
->args
= HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR
) );
132 while (nr
> args
->last
)
133 args
->args
[args
->last
++] = va_arg( *args
->list
, ULONG_PTR
);
135 if (nr
> args
->last
) args
->last
= nr
;
136 return args
->args
[nr
- 1];
139 /**********************************************************************
140 * format_insert (internal)
142 static LPCWSTR
format_insert( BOOL unicode_caller
, int insert
, LPCWSTR format
,
143 DWORD flags
, struct format_args
*args
,
146 static const WCHAR fmt_u
[] = {'%','u',0};
147 WCHAR
*wstring
= NULL
, *p
, fmt
[256];
151 if (*format
!= '!') /* simple string */
153 arg
= get_arg( insert
, flags
, args
);
154 if (unicode_caller
|| !arg
)
156 static const WCHAR nullW
[] = {'(','n','u','l','l',')',0};
157 const WCHAR
*str
= (const WCHAR
*)arg
;
159 if (!str
) str
= nullW
;
160 *result
= HeapAlloc( GetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(WCHAR
) );
161 strcpyW( *result
, str
);
165 const char *str
= (const char *)arg
;
166 DWORD length
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
167 *result
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
168 MultiByteToWideChar( CP_ACP
, 0, str
, -1, *result
, length
);
177 while (*format
== '0' ||
186 p
+= sprintfW( p
, fmt_u
, get_arg( insert
, flags
, args
));
190 else *p
++ = *format
++;
192 while (isdigitW(*format
)) *p
++ = *format
++;
199 p
+= sprintfW( p
, fmt_u
, get_arg( insert
, flags
, args
));
204 while (isdigitW(*format
)) *p
++ = *format
++;
207 /* replicate MS bug: drop an argument when using va_list with width/precision */
208 if (insert
== -1 && args
->list
) args
->last
--;
209 arg
= get_arg( insert
, flags
, args
);
211 /* check for ascii string format */
212 if ((format
[0] == 'h' && format
[1] == 's') ||
213 (format
[0] == 'h' && format
[1] == 'S') ||
214 (unicode_caller
&& format
[0] == 'S') ||
215 (!unicode_caller
&& format
[0] == 's'))
217 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, NULL
, 0 );
218 wstring
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
219 MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, wstring
, len
);
220 arg
= (ULONG_PTR
)wstring
;
223 /* check for ascii character format */
224 else if ((format
[0] == 'h' && format
[1] == 'c') ||
225 (format
[0] == 'h' && format
[1] == 'C') ||
226 (unicode_caller
&& format
[0] == 'C') ||
227 (!unicode_caller
&& format
[0] == 'c'))
230 wstring
= HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR
) );
231 MultiByteToWideChar( CP_ACP
, 0, &ch
, 1, wstring
, 1 );
233 arg
= (ULONG_PTR
)wstring
;
236 /* check for wide string format */
237 else if ((format
[0] == 'l' && format
[1] == 's') ||
238 (format
[0] == 'l' && format
[1] == 'S') ||
239 (format
[0] == 'w' && format
[1] == 's') ||
240 (!unicode_caller
&& format
[0] == 'S'))
244 /* check for wide character format */
245 else if ((format
[0] == 'l' && format
[1] == 'c') ||
246 (format
[0] == 'l' && format
[1] == 'C') ||
247 (format
[0] == 'w' && format
[1] == 'c') ||
248 (!unicode_caller
&& format
[0] == 'C'))
252 /* FIXME: handle I64 etc. */
253 else while (*format
&& *format
!= '!') *p
++ = *format
++;
259 WCHAR
*ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
260 int needed
= snprintfW( ret
, size
, fmt
, arg
);
261 if (needed
== -1 || needed
>= size
)
263 HeapFree( GetProcessHeap(), 0, ret
);
264 size
= max( needed
+ 1, size
* 2 );
273 while (*format
&& *format
!= '!') format
++;
274 if (*format
== '!') format
++;
276 HeapFree( GetProcessHeap(), 0, wstring
);
280 struct _format_message_data
290 static void format_add_char(struct _format_message_data
*fmd
, WCHAR c
)
293 if (fmd
->width
&& fmd
->width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
299 fmd
->inspace
= FALSE
;
304 fmd
->space
= fmd
->t
- 1;
309 fmd
->inspace
= FALSE
;
312 if (fmd
->w
== fmd
->width
) {
315 notspace
= fmd
->space
;
316 while (notspace
!= fmd
->t
&& *notspace
== ' ')
319 notspace
= fmd
->space
= fmd
->t
;
320 fmd
->w
= fmd
->t
- notspace
;
321 memmove(fmd
->space
+2, notspace
, fmd
->w
* sizeof(*fmd
->t
));
322 *fmd
->space
++ = '\r';
323 *fmd
->space
++ = '\n';
324 fmd
->t
= fmd
->space
+ fmd
->w
;
326 fmd
->inspace
= FALSE
;
329 if ((DWORD
)(fmd
->t
- fmd
->formatted
) == fmd
->size
) {
330 DWORD_PTR ispace
= fmd
->space
- fmd
->formatted
;
331 /* Allocate two extra characters so we can insert a '\r\n' in
332 * the middle of a word.
334 fmd
->formatted
= HeapReAlloc(GetProcessHeap(), 0, fmd
->formatted
, (fmd
->size
* 2 + 2) * sizeof(WCHAR
));
335 fmd
->t
= fmd
->formatted
+ fmd
->size
;
337 fmd
->space
= fmd
->formatted
+ ispace
;
342 /**********************************************************************
343 * format_message (internal)
345 static LPWSTR
format_message( BOOL unicode_caller
, DWORD dwFlags
, LPCWSTR fmtstr
,
346 struct format_args
*format_args
)
348 struct _format_message_data fmd
;
353 fmd
.formatted
= fmd
.t
= HeapAlloc( GetProcessHeap(), 0, (fmd
.size
+ 2) * sizeof(WCHAR
) );
355 fmd
.width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
367 case '1':case '2':case '3':case '4':case '5':
368 case '6':case '7':case '8':case '9':
369 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
371 else if (((dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->args
) ||
372 (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->list
))
374 SetLastError(ERROR_INVALID_PARAMETER
);
375 HeapFree(GetProcessHeap(), 0, fmd
.formatted
);
380 case '0':case '1':case '2':case '3':
381 case '4':case '5':case '6':case '7':
384 insertnr
= insertnr
*10 + *f
-'0';
391 f
= format_insert( unicode_caller
, insertnr
, f
, dwFlags
, format_args
, &str
);
392 for (x
= str
; *x
; x
++) format_add_char(&fmd
, *x
);
393 HeapFree( GetProcessHeap(), 0, str
);
396 format_add_char(&fmd
, '\r');
397 format_add_char(&fmd
, '\n');
401 format_add_char(&fmd
, '\r');
405 format_add_char(&fmd
, '\t');
413 SetLastError(ERROR_INVALID_PARAMETER
);
414 HeapFree(GetProcessHeap(), 0, fmd
.formatted
);
418 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
419 format_add_char(&fmd
, '%');
420 format_add_char(&fmd
, *f
++);
430 format_add_char(&fmd
, ' ');
433 format_add_char(&fmd
, '\r');
434 format_add_char(&fmd
, '\n');
440 format_add_char(&fmd
, ' ');
443 format_add_char(&fmd
, '\r');
444 format_add_char(&fmd
, '\n');
448 format_add_char(&fmd
, ch
);
454 return fmd
.formatted
;
457 /***********************************************************************
458 * FormatMessageA (KERNEL32.@)
460 DWORD WINAPI
FormatMessageA(
469 struct format_args format_args
;
475 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
476 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
478 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
482 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
486 *(LPSTR
*)lpBuffer
= NULL
;
489 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
491 format_args
.args
= (ULONG_PTR
*)args
;
492 format_args
.list
= NULL
;
493 format_args
.last
= 0;
497 format_args
.args
= NULL
;
498 format_args
.list
= args
;
499 format_args
.last
= 0;
503 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
505 DWORD length
= MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, NULL
, 0);
506 from
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
507 MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, from
, length
);
509 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
511 from
= search_message( dwFlags
, (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
516 SetLastError(ERROR_INVALID_PARAMETER
);
520 target
= format_message( FALSE
, dwFlags
, from
, &format_args
);
524 TRACE("-- %s\n", debugstr_w(target
));
526 /* Only try writing to an output buffer if there are processed characters
527 * in the temporary output buffer. */
530 destlength
= WideCharToMultiByte(CP_ACP
, 0, target
, -1, NULL
, 0, NULL
, NULL
);
531 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
533 LPSTR buf
= LocalAlloc(LMEM_ZEROINIT
, max(nSize
, destlength
));
534 WideCharToMultiByte(CP_ACP
, 0, target
, -1, buf
, destlength
, NULL
, NULL
);
535 *((LPSTR
*)lpBuffer
) = buf
;
539 if (nSize
< destlength
)
541 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
544 WideCharToMultiByte(CP_ACP
, 0, target
, -1, lpBuffer
, destlength
, NULL
, NULL
);
546 ret
= destlength
- 1; /* null terminator */
550 HeapFree(GetProcessHeap(),0,target
);
551 HeapFree(GetProcessHeap(),0,from
);
552 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
553 TRACE("-- returning %u\n", ret
);
557 /***********************************************************************
558 * FormatMessageW (KERNEL32.@)
560 DWORD WINAPI
FormatMessageW(
569 struct format_args format_args
;
575 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
576 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
580 SetLastError(ERROR_INVALID_PARAMETER
);
584 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
585 *(LPWSTR
*)lpBuffer
= NULL
;
587 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
589 format_args
.args
= (ULONG_PTR
*)args
;
590 format_args
.list
= NULL
;
591 format_args
.last
= 0;
595 format_args
.args
= NULL
;
596 format_args
.list
= args
;
597 format_args
.last
= 0;
601 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
602 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource
) + 1) *
604 strcpyW( from
, lpSource
);
606 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
608 from
= search_message( dwFlags
, (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
613 SetLastError(ERROR_INVALID_PARAMETER
);
617 target
= format_message( TRUE
, dwFlags
, from
, &format_args
);
621 talloced
= strlenW(target
)+1;
622 TRACE("-- %s\n",debugstr_w(target
));
624 /* Only allocate a buffer if there are processed characters in the
625 * temporary output buffer. If a caller supplies the buffer, then
626 * a null terminator will be written to it. */
627 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
631 /* nSize is the MINIMUM size */
632 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
, max(nSize
, talloced
)*sizeof(WCHAR
));
633 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
638 if (nSize
< talloced
)
640 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
643 strcpyW(lpBuffer
, target
);
646 ret
= talloced
- 1; /* null terminator */
648 HeapFree(GetProcessHeap(),0,target
);
649 HeapFree(GetProcessHeap(),0,from
);
650 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
651 TRACE("-- returning %u\n", ret
);