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 );
407 /**********************************************************************
408 * format_messageA (internal)
410 static LPSTR format_messageA( DWORD dwFlags, LPCSTR fmtstr, struct format_args *format_args )
415 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
419 target = t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
422 #define ADD_TO_T(c) do { \
424 if ((DWORD)(t-target) == talloced) {\
425 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
426 t = target+talloced;\
432 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
448 case '1':case '2':case '3':case '4':case '5':
449 case '6':case '7':case '8':case '9':
452 case '0':case '1':case '2':case '3':
453 case '4':case '5':case '6':case '7':
456 insertnr = insertnr*10 + *f-'0';
463 f = format_insertA( insertnr, f, dwFlags, format_args, &str );
464 for (x = str; *x; x++) ADD_TO_T(*x);
465 HeapFree( GetProcessHeap(), 0, str );
516 /**********************************************************************
517 * format_messageW (internal)
519 static LPWSTR format_messageW( DWORD dwFlags, LPCWSTR fmtstr, struct format_args *format_args )
524 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
528 target = t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) );
531 #define ADD_TO_T(c) do {\
533 if ((DWORD)(t-target) == talloced) {\
534 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
535 t = target+talloced;\
541 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
558 case '1':case '2':case '3':case '4':case '5':
559 case '6':case '7':case '8':case '9':
562 case '0':case '1':case '2':case '3':
563 case '4':case '5':case '6':case '7':
566 insertnr = insertnr*10 + *f-'0';
573 f = format_insertW( insertnr, f, dwFlags, format_args, &str );
574 for (x = str; *x; x++) ADD_TO_T(*x);
575 HeapFree( GetProcessHeap(), 0, str );
626 /***********************************************************************
627 * FormatMessageA (KERNEL32.@)
628 * FIXME: missing wrap,
630 DWORD WINAPI FormatMessageA(
639 struct format_args format_args;
644 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
646 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
647 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
648 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
649 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
650 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
652 if ((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) && !lpBuffer)
654 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
658 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
660 format_args.args = (ULONG_PTR *)args;
661 format_args.list = NULL;
662 format_args.last = 0;
666 format_args.args = NULL;
667 format_args.list = args;
668 format_args.last = 0;
671 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
672 FIXME("line wrapping (%u) not supported.\n", width);
674 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
676 from = HeapAlloc( GetProcessHeap(), 0, strlen(lpSource) + 1 );
677 strcpy( from, lpSource );
681 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
682 from = load_messageA( (HMODULE)lpSource, dwMessageId, dwLanguageId );
683 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
684 from = load_messageA( kernel32_handle, dwMessageId, dwLanguageId );
688 target = format_messageA( dwFlags, from, &format_args );
690 talloced = strlen(target)+1;
691 TRACE("-- %s\n",debugstr_a(target));
692 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
693 *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT,max(nSize, talloced));
694 memcpy(*(LPSTR*)lpBuffer,target,talloced);
696 if (nSize < talloced)
698 SetLastError(ERROR_INSUFFICIENT_BUFFER);
701 strcpy(lpBuffer, target);
704 ret = talloced - 1; /* null terminator */
706 HeapFree(GetProcessHeap(),0,target);
707 HeapFree(GetProcessHeap(),0,from);
708 if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
709 TRACE("-- returning %u\n", ret);
715 /***********************************************************************
716 * FormatMessageW (KERNEL32.@)
718 DWORD WINAPI FormatMessageW(
727 struct format_args format_args;
732 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
734 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
735 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
736 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
737 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
738 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
742 SetLastError(ERROR_INVALID_PARAMETER);
746 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
748 format_args.args = (ULONG_PTR *)args;
749 format_args.list = NULL;
750 format_args.last = 0;
754 format_args.args = NULL;
755 format_args.list = args;
756 format_args.last = 0;
759 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
760 FIXME("line wrapping not supported.\n");
762 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
763 from = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource) + 1) *
765 strcpyW( from, lpSource );
769 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
770 from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
771 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
772 from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
776 target = format_messageW( dwFlags, from, &format_args );
778 talloced = strlenW(target)+1;
779 TRACE("-- %s\n",debugstr_w(target));
780 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
781 /* nSize is the MINIMUM size */
782 *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT, max(nSize, talloced)*sizeof(WCHAR));
783 strcpyW(*(LPWSTR*)lpBuffer, target);
787 if (nSize < talloced)
789 SetLastError(ERROR_INSUFFICIENT_BUFFER);
792 strcpyW(lpBuffer, target);
795 ret = talloced - 1; /* null terminator */
797 HeapFree(GetProcessHeap(),0,target);
798 HeapFree(GetProcessHeap(),0,from);
799 if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
800 TRACE("-- returning %u\n", ret);