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
26 DEBUG_CHANNEL(resource
);
28 extern HMODULE kernel32_handle
;
37 /* Messages used by FormatMessage
39 * They can be specified either directly or using a message ID and
40 * loading them from the resource.
42 * The resourcedata has following format:
44 * 0: DWORD nrofentries
45 * nrofentries * subentry:
48 * 8: DWORD offset from start to the stringentries
50 * (lastentry-firstentry) * stringentry:
51 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
54 * (stringentry i of a subentry refers to the ID 'firstentry+i')
56 * Yes, ANSI strings in win32 resources. Go figure.
59 static const WCHAR FMTWSTR
[] = { '%','s',0 };
61 /**********************************************************************
62 * load_message (internal)
64 static LPWSTR
load_message( HMODULE module
, UINT id
, WORD lang
)
66 MESSAGE_RESOURCE_ENTRY
*mre
;
70 TRACE("module = %p, id = %08x\n", module
, id
);
72 if (!module
) module
= GetModuleHandleW( NULL
);
73 if ((status
= RtlFindMessage( module
, (ULONG
)RT_MESSAGETABLE
, lang
, id
, &mre
)) != STATUS_SUCCESS
)
75 SetLastError( RtlNtStatusToDosError(status
) );
79 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
81 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
82 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
83 memcpy( buffer
, mre
->Text
, len
);
87 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
88 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
89 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
91 TRACE("returning %s\n", wine_dbgstr_w(buffer
));
95 /**********************************************************************
98 static ULONG_PTR
get_arg( int nr
, DWORD flags
, struct format_args
*args
)
100 if (nr
== -1) nr
= args
->last
+ 1;
103 if (!args
->args
) args
->args
= HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR
) );
104 while (nr
> args
->last
)
105 args
->args
[args
->last
++] = va_arg( *args
->list
, ULONG_PTR
);
107 if (nr
> args
->last
) args
->last
= nr
;
108 return args
->args
[nr
- 1];
111 /**********************************************************************
112 * format_insert (internal)
114 static LPCWSTR
format_insert( BOOL unicode_caller
, int insert
, LPCWSTR format
,
115 DWORD flags
, struct format_args
*args
,
118 static const WCHAR fmt_u
[] = {'%','u',0};
119 WCHAR
*wstring
= NULL
, *p
, fmt
[256];
123 if (*format
!= '!') /* simple string */
125 arg
= get_arg( insert
, flags
, args
);
126 if (unicode_caller
|| !arg
)
128 static const WCHAR nullW
[] = {'(','n','u','l','l',')',0};
129 const WCHAR
*str
= (const WCHAR
*)arg
;
131 if (!str
) str
= nullW
;
132 *result
= HeapAlloc( GetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(WCHAR
) );
133 strcpyW( *result
, str
);
137 const char *str
= (const char *)arg
;
138 DWORD length
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
139 *result
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
140 MultiByteToWideChar( CP_ACP
, 0, str
, -1, *result
, length
);
149 while (*format
== '0' ||
158 p
+= sprintfW( p
, fmt_u
, get_arg( insert
, flags
, args
));
162 else *p
++ = *format
++;
164 while (isdigitW(*format
)) *p
++ = *format
++;
171 p
+= sprintfW( p
, fmt_u
, get_arg( insert
, flags
, args
));
176 while (isdigitW(*format
)) *p
++ = *format
++;
179 /* replicate MS bug: drop an argument when using va_list with width/precision */
180 if (insert
== -1 && args
->list
) args
->last
--;
181 arg
= get_arg( insert
, flags
, args
);
183 /* check for ascii string format */
184 if ((format
[0] == 'h' && format
[1] == 's') ||
185 (format
[0] == 'h' && format
[1] == 'S') ||
186 (unicode_caller
&& format
[0] == 'S') ||
187 (!unicode_caller
&& format
[0] == 's'))
189 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, NULL
, 0 );
190 wstring
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
191 MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, wstring
, len
);
192 arg
= (ULONG_PTR
)wstring
;
195 /* check for ascii character format */
196 else if ((format
[0] == 'h' && format
[1] == 'c') ||
197 (format
[0] == 'h' && format
[1] == 'C') ||
198 (unicode_caller
&& format
[0] == 'C') ||
199 (!unicode_caller
&& format
[0] == 'c'))
202 wstring
= HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR
) );
203 MultiByteToWideChar( CP_ACP
, 0, &ch
, 1, wstring
, 1 );
205 arg
= (ULONG_PTR
)wstring
;
208 /* check for wide string format */
209 else if ((format
[0] == 'l' && format
[1] == 's') ||
210 (format
[0] == 'l' && format
[1] == 'S') ||
211 (format
[0] == 'w' && format
[1] == 's') ||
212 (!unicode_caller
&& format
[0] == 'S'))
216 /* check for wide character format */
217 else if ((format
[0] == 'l' && format
[1] == 'c') ||
218 (format
[0] == 'l' && format
[1] == 'C') ||
219 (format
[0] == 'w' && format
[1] == 'c') ||
220 (!unicode_caller
&& format
[0] == 'C'))
224 /* FIXME: handle I64 etc. */
225 else while (*format
&& *format
!= '!') *p
++ = *format
++;
231 WCHAR
*ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
232 int needed
= snprintfW( ret
, size
, fmt
, arg
);
233 if (needed
== -1 || needed
>= size
)
235 HeapFree( GetProcessHeap(), 0, ret
);
236 size
= max( needed
+ 1, size
* 2 );
245 while (*format
&& *format
!= '!') format
++;
246 if (*format
== '!') format
++;
248 HeapFree( GetProcessHeap(), 0, wstring
);
252 struct _format_message_data
262 static void format_add_char(struct _format_message_data
*fmd
, WCHAR c
)
265 if (fmd
->width
&& fmd
->width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
271 fmd
->inspace
= FALSE
;
276 fmd
->space
= fmd
->t
- 1;
281 fmd
->inspace
= FALSE
;
284 if (fmd
->w
== fmd
->width
) {
287 notspace
= fmd
->space
;
288 while (notspace
!= fmd
->t
&& *notspace
== ' ')
291 notspace
= fmd
->space
= fmd
->t
;
292 fmd
->w
= fmd
->t
- notspace
;
293 memmove(fmd
->space
+2, notspace
, fmd
->w
* sizeof(*fmd
->t
));
294 *fmd
->space
++ = '\r';
295 *fmd
->space
++ = '\n';
296 fmd
->t
= fmd
->space
+ fmd
->w
;
298 fmd
->inspace
= FALSE
;
301 if ((DWORD
)(fmd
->t
- fmd
->formatted
) == fmd
->size
) {
302 DWORD_PTR ispace
= fmd
->space
- fmd
->formatted
;
303 /* Allocate two extra characters so we can insert a '\r\n' in
304 * the middle of a word.
306 fmd
->formatted
= HeapReAlloc(GetProcessHeap(), 0, fmd
->formatted
, (fmd
->size
* 2 + 2) * sizeof(WCHAR
));
307 fmd
->t
= fmd
->formatted
+ fmd
->size
;
309 fmd
->space
= fmd
->formatted
+ ispace
;
314 /**********************************************************************
315 * format_message (internal)
317 static LPWSTR
format_message( BOOL unicode_caller
, DWORD dwFlags
, LPCWSTR fmtstr
,
318 struct format_args
*format_args
)
320 struct _format_message_data fmd
;
325 fmd
.formatted
= fmd
.t
= HeapAlloc( GetProcessHeap(), 0, (fmd
.size
+ 2) * sizeof(WCHAR
) );
327 fmd
.width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
339 case '1':case '2':case '3':case '4':case '5':
340 case '6':case '7':case '8':case '9':
341 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
343 else if (((dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->args
) ||
344 (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->list
))
346 SetLastError(ERROR_INVALID_PARAMETER
);
347 HeapFree(GetProcessHeap(), 0, fmd
.formatted
);
352 case '0':case '1':case '2':case '3':
353 case '4':case '5':case '6':case '7':
356 insertnr
= insertnr
*10 + *f
-'0';
363 f
= format_insert( unicode_caller
, insertnr
, f
, dwFlags
, format_args
, &str
);
364 for (x
= str
; *x
; x
++) format_add_char(&fmd
, *x
);
365 HeapFree( GetProcessHeap(), 0, str
);
368 format_add_char(&fmd
, '\r');
369 format_add_char(&fmd
, '\n');
373 format_add_char(&fmd
, '\r');
377 format_add_char(&fmd
, '\t');
385 SetLastError(ERROR_INVALID_PARAMETER
);
386 HeapFree(GetProcessHeap(), 0, fmd
.formatted
);
390 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
391 format_add_char(&fmd
, '%');
392 format_add_char(&fmd
, *f
++);
402 format_add_char(&fmd
, ' ');
405 format_add_char(&fmd
, '\r');
406 format_add_char(&fmd
, '\n');
412 format_add_char(&fmd
, ' ');
415 format_add_char(&fmd
, '\r');
416 format_add_char(&fmd
, '\n');
420 format_add_char(&fmd
, ch
);
426 return fmd
.formatted
;
429 /***********************************************************************
430 * FormatMessageA (KERNEL32.@)
432 DWORD WINAPI
FormatMessageA(
441 struct format_args format_args
;
447 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
448 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
450 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
454 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
458 *(LPSTR
*)lpBuffer
= NULL
;
461 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
463 format_args
.args
= (ULONG_PTR
*)args
;
464 format_args
.list
= NULL
;
465 format_args
.last
= 0;
469 format_args
.args
= NULL
;
470 format_args
.list
= args
;
471 format_args
.last
= 0;
475 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
477 DWORD length
= MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, NULL
, 0);
478 from
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
479 MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, from
, length
);
481 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
483 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
484 from
= load_message( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
485 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
486 from
= load_message( kernel32_handle
, dwMessageId
, dwLanguageId
);
491 SetLastError(ERROR_INVALID_PARAMETER
);
495 target
= format_message( FALSE
, dwFlags
, from
, &format_args
);
499 TRACE("-- %s\n", debugstr_w(target
));
501 /* Only try writing to an output buffer if there are processed characters
502 * in the temporary output buffer. */
505 destlength
= WideCharToMultiByte(CP_ACP
, 0, target
, -1, NULL
, 0, NULL
, NULL
);
506 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
508 LPSTR buf
= LocalAlloc(LMEM_ZEROINIT
, max(nSize
, destlength
));
509 WideCharToMultiByte(CP_ACP
, 0, target
, -1, buf
, destlength
, NULL
, NULL
);
510 *((LPSTR
*)lpBuffer
) = buf
;
514 if (nSize
< destlength
)
516 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
519 WideCharToMultiByte(CP_ACP
, 0, target
, -1, lpBuffer
, destlength
, NULL
, NULL
);
521 ret
= destlength
- 1; /* null terminator */
525 HeapFree(GetProcessHeap(),0,target
);
526 HeapFree(GetProcessHeap(),0,from
);
527 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
528 TRACE("-- returning %u\n", ret
);
532 /***********************************************************************
533 * FormatMessageW (KERNEL32.@)
535 DWORD WINAPI
FormatMessageW(
544 struct format_args format_args
;
550 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
551 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
555 SetLastError(ERROR_INVALID_PARAMETER
);
559 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
560 *(LPWSTR
*)lpBuffer
= NULL
;
562 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
564 format_args
.args
= (ULONG_PTR
*)args
;
565 format_args
.list
= NULL
;
566 format_args
.last
= 0;
570 format_args
.args
= NULL
;
571 format_args
.list
= args
;
572 format_args
.last
= 0;
576 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
577 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource
) + 1) *
579 strcpyW( from
, lpSource
);
581 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
583 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
584 from
= load_message( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
585 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
586 from
= load_message( kernel32_handle
, dwMessageId
, dwLanguageId
);
591 SetLastError(ERROR_INVALID_PARAMETER
);
595 target
= format_message( TRUE
, dwFlags
, from
, &format_args
);
599 talloced
= strlenW(target
)+1;
600 TRACE("-- %s\n",debugstr_w(target
));
602 /* Only allocate a buffer if there are processed characters in the
603 * temporary output buffer. If a caller supplies the buffer, then
604 * a null terminator will be written to it. */
605 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
609 /* nSize is the MINIMUM size */
610 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
, max(nSize
, talloced
)*sizeof(WCHAR
));
611 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
616 if (nSize
< talloced
)
618 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
621 strcpyW(lpBuffer
, target
);
624 ret
= talloced
- 1; /* null terminator */
626 HeapFree(GetProcessHeap(),0,target
);
627 HeapFree(GetProcessHeap(),0,from
);
628 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
629 TRACE("-- returning %u\n", ret
);