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 FMTWSTR
[] = { '%','s',0 };
73 /**********************************************************************
74 * load_message (internal)
76 static LPWSTR
load_message( HMODULE module
, UINT id
, WORD lang
)
78 const MESSAGE_RESOURCE_ENTRY
*mre
;
82 TRACE("module = %p, id = %08x\n", module
, id
);
84 if (!module
) module
= GetModuleHandleW( NULL
);
85 if ((status
= RtlFindMessage( module
, RT_MESSAGETABLE
, lang
, id
, &mre
)) != STATUS_SUCCESS
)
87 SetLastError( RtlNtStatusToDosError(status
) );
91 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
93 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
94 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
95 memcpy( buffer
, mre
->Text
, len
);
99 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
100 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
101 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
103 TRACE("returning %s\n", wine_dbgstr_w(buffer
));
107 /**********************************************************************
110 static ULONG_PTR
get_arg( int nr
, DWORD flags
, struct format_args
*args
)
112 if (nr
== -1) nr
= args
->last
+ 1;
115 if (!args
->args
) args
->args
= HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR
) );
116 while (nr
> args
->last
)
117 args
->args
[args
->last
++] = va_arg( *args
->list
, ULONG_PTR
);
119 if (nr
> args
->last
) args
->last
= nr
;
120 return args
->args
[nr
- 1];
123 /**********************************************************************
124 * format_insert (internal)
126 static LPCWSTR
format_insert( BOOL unicode_caller
, int insert
, LPCWSTR format
,
127 DWORD flags
, struct format_args
*args
,
130 static const WCHAR fmt_lu
[] = {'%','l','u',0};
131 WCHAR
*wstring
= NULL
, *p
, fmt
[256];
135 if (*format
!= '!') /* simple string */
137 arg
= get_arg( insert
, flags
, args
);
140 WCHAR
*str
= (WCHAR
*)arg
;
141 *result
= HeapAlloc( GetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(WCHAR
) );
142 strcpyW( *result
, str
);
146 char *str
= (char *)arg
;
147 DWORD length
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
148 *result
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
149 MultiByteToWideChar( CP_ACP
, 0, str
, -1, *result
, length
);
158 while (*format
== '0' ||
167 p
+= sprintfW( p
, fmt_lu
, get_arg( insert
, flags
, args
));
171 else *p
++ = *format
++;
173 while (isdigitW(*format
)) *p
++ = *format
++;
180 p
+= sprintfW( p
, fmt_lu
, get_arg( insert
, flags
, args
));
185 while (isdigitW(*format
)) *p
++ = *format
++;
188 /* replicate MS bug: drop an argument when using va_list with width/precision */
189 if (insert
== -1 && args
->list
) args
->last
--;
190 arg
= get_arg( insert
, flags
, args
);
192 /* check for ascii string format */
193 if ((format
[0] == 'h' && format
[1] == 's') ||
194 (format
[0] == 'h' && format
[1] == 'S') ||
195 (unicode_caller
&& format
[0] == 'S') ||
196 (!unicode_caller
&& format
[0] == 's'))
198 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, NULL
, 0 );
199 wstring
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
200 MultiByteToWideChar( CP_ACP
, 0, (char *)arg
, -1, wstring
, len
);
201 arg
= (ULONG_PTR
)wstring
;
204 /* check for ascii character format */
205 else if ((format
[0] == 'h' && format
[1] == 'c') ||
206 (format
[0] == 'h' && format
[1] == 'C') ||
207 (unicode_caller
&& format
[0] == 'C') ||
208 (!unicode_caller
&& format
[0] == 'c'))
211 wstring
= HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR
) );
212 MultiByteToWideChar( CP_ACP
, 0, &ch
, 1, wstring
, 1 );
214 arg
= (ULONG_PTR
)wstring
;
217 /* check for wide string format */
218 else if ((format
[0] == 'l' && format
[1] == 's') ||
219 (format
[0] == 'l' && format
[1] == 'S') ||
220 (format
[0] == 'w' && format
[1] == 's') ||
221 (!unicode_caller
&& format
[0] == 'S'))
225 /* check for wide character format */
226 else if ((format
[0] == 'l' && format
[1] == 'c') ||
227 (format
[0] == 'l' && format
[1] == 'C') ||
228 (format
[0] == 'w' && format
[1] == 'c') ||
229 (!unicode_caller
&& format
[0] == 'C'))
233 /* FIXME: handle I64 etc. */
234 else while (*format
&& *format
!= '!') *p
++ = *format
++;
240 WCHAR
*ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
241 int needed
= snprintfW( ret
, size
, fmt
, arg
);
242 if (needed
== -1 || needed
>= size
)
244 HeapFree( GetProcessHeap(), 0, ret
);
245 size
= max( needed
+ 1, size
* 2 );
254 while (*format
&& *format
!= '!') format
++;
255 if (*format
== '!') format
++;
257 HeapFree( GetProcessHeap(), 0, wstring
);
261 /**********************************************************************
262 * format_message (internal)
264 static LPWSTR
format_message( BOOL unicode_caller
, DWORD dwFlags
, LPCWSTR fmtstr
,
265 struct format_args
*format_args
)
270 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
274 target
= t
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
277 #define ADD_TO_T(c) do {\
279 if ((DWORD)(t-target) == talloced) {\
280 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
281 t = target+talloced;\
294 case '1':case '2':case '3':case '4':case '5':
295 case '6':case '7':case '8':case '9':
296 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
298 else if (((dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->args
) ||
299 (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->list
))
301 SetLastError(ERROR_INVALID_PARAMETER
);
302 HeapFree(GetProcessHeap(), 0, target
);
307 case '0':case '1':case '2':case '3':
308 case '4':case '5':case '6':case '7':
311 insertnr
= insertnr
*10 + *f
-'0';
318 f
= format_insert( unicode_caller
, insertnr
, f
, dwFlags
, format_args
, &str
);
319 for (x
= str
; *x
; x
++) ADD_TO_T(*x
);
320 HeapFree( GetProcessHeap(), 0, str
);
340 SetLastError(ERROR_INVALID_PARAMETER
);
341 HeapFree(GetProcessHeap(), 0, target
);
345 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
385 /***********************************************************************
386 * FormatMessageA (KERNEL32.@)
387 * FIXME: missing wrap,
389 DWORD WINAPI
FormatMessageA(
398 struct format_args format_args
;
403 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
405 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
406 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
408 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
412 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
416 *(LPSTR
*)lpBuffer
= NULL
;
419 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
421 format_args
.args
= (ULONG_PTR
*)args
;
422 format_args
.list
= NULL
;
423 format_args
.last
= 0;
427 format_args
.args
= NULL
;
428 format_args
.list
= args
;
429 format_args
.last
= 0;
432 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
433 FIXME("line wrapping (%u) not supported.\n", width
);
435 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
437 DWORD length
= MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, NULL
, 0);
438 from
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
439 MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, from
, length
);
441 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
443 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
444 from
= load_message( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
445 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
446 from
= load_message( kernel32_handle
, dwMessageId
, dwLanguageId
);
451 SetLastError(ERROR_INVALID_PARAMETER
);
455 target
= format_message( FALSE
, dwFlags
, from
, &format_args
);
459 TRACE("-- %s\n", debugstr_w(target
));
461 /* Only try writing to an output buffer if there are processed characters
462 * in the temporary output buffer. */
465 destlength
= WideCharToMultiByte(CP_ACP
, 0, target
, -1, NULL
, 0, NULL
, NULL
);
466 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
468 LPSTR buf
= LocalAlloc(LMEM_ZEROINIT
, max(nSize
, destlength
));
469 WideCharToMultiByte(CP_ACP
, 0, target
, -1, buf
, destlength
, NULL
, NULL
);
470 *((LPSTR
*)lpBuffer
) = buf
;
474 if (nSize
< destlength
)
476 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
479 WideCharToMultiByte(CP_ACP
, 0, target
, -1, lpBuffer
, destlength
, NULL
, NULL
);
481 ret
= destlength
- 1; /* null terminator */
485 HeapFree(GetProcessHeap(),0,target
);
486 HeapFree(GetProcessHeap(),0,from
);
487 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
488 TRACE("-- returning %u\n", ret
);
492 /***********************************************************************
493 * FormatMessageW (KERNEL32.@)
495 DWORD WINAPI
FormatMessageW(
504 struct format_args format_args
;
509 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
511 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
512 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
516 SetLastError(ERROR_INVALID_PARAMETER
);
520 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
521 *(LPWSTR
*)lpBuffer
= NULL
;
523 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
525 format_args
.args
= (ULONG_PTR
*)args
;
526 format_args
.list
= NULL
;
527 format_args
.last
= 0;
531 format_args
.args
= NULL
;
532 format_args
.list
= args
;
533 format_args
.last
= 0;
536 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
537 FIXME("line wrapping not supported.\n");
539 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
540 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource
) + 1) *
542 strcpyW( from
, lpSource
);
544 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
546 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
547 from
= load_message( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
548 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
549 from
= load_message( kernel32_handle
, dwMessageId
, dwLanguageId
);
554 SetLastError(ERROR_INVALID_PARAMETER
);
558 target
= format_message( TRUE
, dwFlags
, from
, &format_args
);
562 talloced
= strlenW(target
)+1;
563 TRACE("-- %s\n",debugstr_w(target
));
565 /* Only allocate a buffer if there are processed characters in the
566 * temporary output buffer. If a caller supplies the buffer, then
567 * a null terminator will be written to it. */
568 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
572 /* nSize is the MINIMUM size */
573 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
, max(nSize
, talloced
)*sizeof(WCHAR
));
574 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
579 if (nSize
< talloced
)
581 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
584 strcpyW(lpBuffer
, target
);
587 ret
= talloced
- 1; /* null terminator */
589 HeapFree(GetProcessHeap(),0,target
);
590 HeapFree(GetProcessHeap(),0,from
);
591 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
592 TRACE("-- returning %u\n", ret
);