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));
108 /**********************************************************************
111 static ULONG_PTR get_arg( int nr, DWORD flags, struct format_args *args )
113 if (nr == -1) nr = args->last + 1;
116 if (!args->args) args->args = HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR) );
117 while (nr > args->last)
118 args->args[args->last++] = va_arg( *args->list, ULONG_PTR );
120 if (nr > args->last) args->last = nr;
121 return args->args[nr - 1];
125 /**********************************************************************
126 * format_insertA (internal)
128 static LPCWSTR format_insertA( int insert, LPCWSTR format, DWORD flags,
129 struct format_args *args, LPWSTR *result )
131 static const WCHAR fmt_lu[] = {'%','l','u',0};
132 WCHAR *wstring = NULL, *p, fmt[256];
136 if (*format != '!') /* simple string */
138 char *str = (char *)get_arg( insert, flags, args );
139 DWORD length = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
140 *result = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
141 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 (isdigit(*format)) *p++ = *format++;
171 p += sprintfW( p, fmt_lu, get_arg( insert, flags, args ));
176 while (isdigit(*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 wide string format */
184 if ((format[0] == 'l' && format[1] == 's') ||
185 (format[0] == 'l' && format[1] == 'S') ||
186 (format[0] == 'w' && format[1] == 's') ||
191 /* check for wide character format */
192 else if ((format[0] == 'l' && format[1] == 'c') ||
193 (format[0] == 'l' && format[1] == 'C') ||
194 (format[0] == 'w' && format[1] == 'c') ||
199 /* check for ascii string format */
200 else if ((format[0] == 'h' && format[1] == 's') ||
201 (format[0] == 'h' && format[1] == 'S') ||
204 DWORD len = MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, NULL, 0 );
205 wstring = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
206 MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, wstring, len );
207 arg = (ULONG_PTR)wstring;
210 /* check for ascii character format */
211 else if ((format[0] == 'h' && format[1] == 'c') ||
212 (format[0] == 'h' && format[1] == 'C') ||
216 wstring = HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR) );
217 MultiByteToWideChar( CP_ACP, 0, &ch, 1, wstring, 1 );
219 arg = (ULONG_PTR)wstring;
222 /* FIXME: handle I64 etc. */
223 else while (*format && *format != '!') *p++ = *format++;
229 WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR));
230 int needed = snprintfW( ret, size, fmt, arg );
231 if (needed == -1 || needed >= size)
233 HeapFree( GetProcessHeap(), 0, ret );
234 size = max( needed + 1, size * 2 );
243 while (*format && *format != '!') format++;
244 if (*format == '!') format++;
246 HeapFree( GetProcessHeap(), 0, wstring );
251 /**********************************************************************
252 * format_insertW (internal)
254 static LPCWSTR format_insertW( int insert, LPCWSTR format, DWORD flags,
255 struct format_args *args, LPWSTR *result )
257 static const WCHAR fmt_lu[] = {'%','l','u',0};
258 WCHAR *wstring = NULL, *p, fmt[256];
262 if (*format != '!') /* simple string */
264 WCHAR *str = (WCHAR *)get_arg( insert, flags, args );
265 *result = HeapAlloc( GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR) );
266 strcpyW( *result, str );
274 while (*format == '0' ||
283 p += sprintfW( p, fmt_lu, get_arg( insert, flags, args ));
287 else *p++ = *format++;
289 while (isdigitW(*format)) *p++ = *format++;
296 p += sprintfW( p, fmt_lu, get_arg( insert, flags, args ));
301 while (isdigitW(*format)) *p++ = *format++;
304 /* replicate MS bug: drop an argument when using va_list with width/precision */
305 if (insert == -1 && args->list) args->last--;
306 arg = get_arg( insert, flags, args );
308 /* check for ascii string format */
309 if ((format[0] == 'h' && format[1] == 's') ||
310 (format[0] == 'h' && format[1] == 'S') ||
313 DWORD len = MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, /*FIXME*/ NULL, 0 );
314 wstring = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
315 MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, wstring, len );
316 arg = (ULONG_PTR)wstring;
319 /* check for ascii character format */
320 else if ((format[0] == 'h' && format[1] == 'c') ||
321 (format[0] == 'h' && format[1] == 'C') ||
325 wstring = HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR) );
326 MultiByteToWideChar( CP_ACP, 0, &ch, 1, wstring, 1 );
328 arg = (ULONG_PTR)wstring;
331 /* check for wide string format */
332 else if ((format[0] == 'l' && format[1] == 's') ||
333 (format[0] == 'l' && format[1] == 'S') ||
334 (format[0] == 'w' && format[1] == 's'))
338 /* check for wide character format */
339 else if ((format[0] == 'l' && format[1] == 'c') ||
340 (format[0] == 'l' && format[1] == 'C') ||
341 (format[0] == 'w' && format[1] == 'c'))
345 /* FIXME: handle I64 etc. */
346 else while (*format && *format != '!') *p++ = *format++;
352 WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
353 int needed = snprintfW( ret, size, fmt, arg );
354 if (needed == -1 || needed >= size)
356 HeapFree( GetProcessHeap(), 0, ret );
357 size = max( needed + 1, size * 2 );
366 while (*format && *format != '!') format++;
367 if (*format == '!') format++;
369 HeapFree( GetProcessHeap(), 0, wstring );
373 /**********************************************************************
374 * format_messageA (internal)
376 static LPWSTR format_messageA( DWORD dwFlags, LPCWSTR fmtstr, struct format_args *format_args )
381 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
385 target = t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR));
388 #define ADD_TO_T(c) do { \
390 if ((DWORD)(t-target) == talloced) {\
391 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
392 t = target+talloced;\
398 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
414 case '1':case '2':case '3':case '4':case '5':
415 case '6':case '7':case '8':case '9':
418 case '0':case '1':case '2':case '3':
419 case '4':case '5':case '6':case '7':
422 insertnr = insertnr*10 + *f-'0';
429 f = format_insertA( insertnr, f, dwFlags, format_args, &str );
430 for (x = str; *x; x++) ADD_TO_T(*x);
431 HeapFree( GetProcessHeap(), 0, str );
482 /**********************************************************************
483 * format_messageW (internal)
485 static LPWSTR format_messageW( DWORD dwFlags, LPCWSTR fmtstr, struct format_args *format_args )
490 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
494 target = t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) );
497 #define ADD_TO_T(c) do {\
499 if ((DWORD)(t-target) == talloced) {\
500 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
501 t = target+talloced;\
507 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
524 case '1':case '2':case '3':case '4':case '5':
525 case '6':case '7':case '8':case '9':
528 case '0':case '1':case '2':case '3':
529 case '4':case '5':case '6':case '7':
532 insertnr = insertnr*10 + *f-'0';
539 f = format_insertW( insertnr, f, dwFlags, format_args, &str );
540 for (x = str; *x; x++) ADD_TO_T(*x);
541 HeapFree( GetProcessHeap(), 0, str );
592 /***********************************************************************
593 * FormatMessageA (KERNEL32.@)
594 * FIXME: missing wrap,
596 DWORD WINAPI FormatMessageA(
605 struct format_args format_args;
610 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
612 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
613 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
614 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
615 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
616 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
618 if ((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) && !lpBuffer)
620 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
624 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
626 format_args.args = (ULONG_PTR *)args;
627 format_args.list = NULL;
628 format_args.last = 0;
632 format_args.args = NULL;
633 format_args.list = args;
634 format_args.last = 0;
637 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
638 FIXME("line wrapping (%u) not supported.\n", width);
640 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
642 DWORD length = MultiByteToWideChar(CP_ACP, 0, lpSource, -1, NULL, 0);
643 from = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
644 MultiByteToWideChar(CP_ACP, 0, lpSource, -1, from, length);
647 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
648 from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
649 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
650 from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
654 target = format_messageA( dwFlags, from, &format_args );
656 TRACE("-- %s\n", debugstr_w(target));
657 destlength = WideCharToMultiByte(CP_ACP, 0, target, -1, NULL, 0, NULL, NULL);
658 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
659 LPSTR buf = LocalAlloc(LMEM_ZEROINIT, max(nSize, destlength));
660 WideCharToMultiByte(CP_ACP, 0, target, -1, buf, destlength, NULL, NULL);
661 *((LPSTR*)lpBuffer) = buf;
663 if (nSize < destlength)
665 SetLastError(ERROR_INSUFFICIENT_BUFFER);
669 WideCharToMultiByte(CP_ACP, 0, target, -1, lpBuffer, destlength, NULL, NULL);
672 ret = destlength - 1; /* null terminator */
674 HeapFree(GetProcessHeap(),0,target);
675 HeapFree(GetProcessHeap(),0,from);
676 if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
677 TRACE("-- returning %u\n", ret);
683 /***********************************************************************
684 * FormatMessageW (KERNEL32.@)
686 DWORD WINAPI FormatMessageW(
695 struct format_args format_args;
700 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
702 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
703 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
704 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
705 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
706 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
710 SetLastError(ERROR_INVALID_PARAMETER);
714 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
716 format_args.args = (ULONG_PTR *)args;
717 format_args.list = NULL;
718 format_args.last = 0;
722 format_args.args = NULL;
723 format_args.list = args;
724 format_args.last = 0;
727 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
728 FIXME("line wrapping not supported.\n");
730 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
731 from = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource) + 1) *
733 strcpyW( from, lpSource );
737 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
738 from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
739 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
740 from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
744 target = format_messageW( dwFlags, from, &format_args );
746 talloced = strlenW(target)+1;
747 TRACE("-- %s\n",debugstr_w(target));
748 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
749 /* nSize is the MINIMUM size */
750 *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT, max(nSize, talloced)*sizeof(WCHAR));
751 strcpyW(*(LPWSTR*)lpBuffer, target);
755 if (nSize < talloced)
757 SetLastError(ERROR_INSUFFICIENT_BUFFER);
760 strcpyW(lpBuffer, target);
763 ret = talloced - 1; /* null terminator */
765 HeapFree(GetProcessHeap(),0,target);
766 HeapFree(GetProcessHeap(),0,from);
767 if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
768 TRACE("-- returning %u\n", ret);