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_lu
[] = {'%','l','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_lu
, get_arg( insert
, flags
, args
));
162 else *p
++ = *format
++;
164 while (isdigitW(*format
)) *p
++ = *format
++;
171 p
+= sprintfW( p
, fmt_lu
, 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 /**********************************************************************
253 * format_message (internal)
255 static LPWSTR
format_message( BOOL unicode_caller
, DWORD dwFlags
, LPCWSTR fmtstr
,
256 struct format_args
*format_args
)
261 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
265 target
= t
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
268 #define ADD_TO_T(c) do {\
270 if ((DWORD)(t-target) == talloced) {\
271 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
272 t = target+talloced;\
285 case '1':case '2':case '3':case '4':case '5':
286 case '6':case '7':case '8':case '9':
287 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
289 else if (((dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->args
) ||
290 (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
) && !format_args
->list
))
292 SetLastError(ERROR_INVALID_PARAMETER
);
293 HeapFree(GetProcessHeap(), 0, target
);
298 case '0':case '1':case '2':case '3':
299 case '4':case '5':case '6':case '7':
302 insertnr
= insertnr
*10 + *f
-'0';
309 f
= format_insert( unicode_caller
, insertnr
, f
, dwFlags
, format_args
, &str
);
310 for (x
= str
; *x
; x
++) ADD_TO_T(*x
);
311 HeapFree( GetProcessHeap(), 0, str
);
331 SetLastError(ERROR_INVALID_PARAMETER
);
332 HeapFree(GetProcessHeap(), 0, target
);
336 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
)
376 /***********************************************************************
377 * FormatMessageA (KERNEL32.@)
378 * FIXME: missing wrap,
380 DWORD WINAPI
FormatMessageA(
389 struct format_args format_args
;
394 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
396 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
397 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
399 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
403 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
407 *(LPSTR
*)lpBuffer
= NULL
;
410 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
412 format_args
.args
= (ULONG_PTR
*)args
;
413 format_args
.list
= NULL
;
414 format_args
.last
= 0;
418 format_args
.args
= NULL
;
419 format_args
.list
= args
;
420 format_args
.last
= 0;
423 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
424 FIXME("line wrapping (%u) not supported.\n", width
);
426 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
428 DWORD length
= MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, NULL
, 0);
429 from
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) );
430 MultiByteToWideChar(CP_ACP
, 0, lpSource
, -1, from
, length
);
432 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
434 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
435 from
= load_message( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
436 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
437 from
= load_message( kernel32_handle
, dwMessageId
, (WORD
)dwLanguageId
);
442 SetLastError(ERROR_INVALID_PARAMETER
);
446 target
= format_message( FALSE
, dwFlags
, from
, &format_args
);
450 TRACE("-- %s\n", debugstr_w(target
));
452 /* Only try writing to an output buffer if there are processed characters
453 * in the temporary output buffer. */
456 destlength
= WideCharToMultiByte(CP_ACP
, 0, target
, -1, NULL
, 0, NULL
, NULL
);
457 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
459 LPSTR buf
= LocalAlloc(LMEM_ZEROINIT
, max(nSize
, destlength
));
460 WideCharToMultiByte(CP_ACP
, 0, target
, -1, buf
, destlength
, NULL
, NULL
);
461 *((LPSTR
*)lpBuffer
) = buf
;
465 if (nSize
< destlength
)
467 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
470 WideCharToMultiByte(CP_ACP
, 0, target
, -1, lpBuffer
, destlength
, NULL
, NULL
);
472 ret
= destlength
- 1; /* null terminator */
476 HeapFree(GetProcessHeap(),0,target
);
477 HeapFree(GetProcessHeap(),0,from
);
478 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
479 TRACE("-- returning %u\n", ret
);
483 /***********************************************************************
484 * FormatMessageW (KERNEL32.@)
486 DWORD WINAPI
FormatMessageW(
495 struct format_args format_args
;
500 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
502 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
503 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
507 SetLastError(ERROR_INVALID_PARAMETER
);
511 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
512 *(LPWSTR
*)lpBuffer
= NULL
;
514 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
516 format_args
.args
= (ULONG_PTR
*)args
;
517 format_args
.list
= NULL
;
518 format_args
.last
= 0;
522 format_args
.args
= NULL
;
523 format_args
.list
= args
;
524 format_args
.last
= 0;
527 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
528 FIXME("line wrapping not supported.\n");
530 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
531 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource
) + 1) *
533 strcpyW( from
, lpSource
);
535 else if (dwFlags
& (FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
))
537 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
538 from
= load_message( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
539 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
540 from
= load_message( kernel32_handle
, dwMessageId
, (WORD
)dwLanguageId
);
545 SetLastError(ERROR_INVALID_PARAMETER
);
549 target
= format_message( TRUE
, dwFlags
, from
, &format_args
);
553 talloced
= strlenW(target
)+1;
554 TRACE("-- %s\n",debugstr_w(target
));
556 /* Only allocate a buffer if there are processed characters in the
557 * temporary output buffer. If a caller supplies the buffer, then
558 * a null terminator will be written to it. */
559 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
)
563 /* nSize is the MINIMUM size */
564 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
, max(nSize
, talloced
)*sizeof(WCHAR
));
565 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
570 if (nSize
< talloced
)
572 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
575 strcpyW(lpBuffer
, target
);
578 ret
= talloced
- 1; /* null terminator */
580 HeapFree(GetProcessHeap(),0,target
);
581 HeapFree(GetProcessHeap(),0,from
);
582 if (!(dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)) HeapFree( GetProcessHeap(), 0, format_args
.args
);
583 TRACE("-- returning %u\n", ret
);