2000-07-25 19:53:58 +02:00
|
|
|
/*
|
|
|
|
* FormatMessage implementation
|
|
|
|
*
|
|
|
|
* Copyright 1996 Marcus Meissner
|
2009-10-29 17:23:00 +01:00
|
|
|
* Copyright 2009 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-07-25 19:53:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-07-25 19:53:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#define WIN32_NO_STATUS
|
2000-07-25 19:53:58 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
2003-05-07 05:19:17 +02:00
|
|
|
#include "winternl.h"
|
2001-10-10 04:51:24 +02:00
|
|
|
#include "winuser.h"
|
2000-07-25 19:53:58 +02:00
|
|
|
#include "winnls.h"
|
2000-08-14 16:41:19 +02:00
|
|
|
#include "wine/unicode.h"
|
2004-02-12 01:00:55 +01:00
|
|
|
#include "kernel_private.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(resource);
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2009-10-29 19:11:28 +01:00
|
|
|
struct format_args
|
|
|
|
{
|
|
|
|
ULONG_PTR *args;
|
|
|
|
__ms_va_list *list;
|
|
|
|
int last;
|
|
|
|
};
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2008-11-25 17:42:38 +01:00
|
|
|
/* Messages used by FormatMessage
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
2000-07-25 19:53:58 +02:00
|
|
|
* They can be specified either directly or using a message ID and
|
|
|
|
* loading them from the resource.
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
2000-07-25 19:53:58 +02:00
|
|
|
* The resourcedata has following format:
|
|
|
|
* start:
|
|
|
|
* 0: DWORD nrofentries
|
|
|
|
* nrofentries * subentry:
|
|
|
|
* 0: DWORD firstentry
|
|
|
|
* 4: DWORD lastentry
|
|
|
|
* 8: DWORD offset from start to the stringentries
|
|
|
|
*
|
|
|
|
* (lastentry-firstentry) * stringentry:
|
2000-08-02 02:56:28 +02:00
|
|
|
* 0: WORD len (0 marks end) [ includes the 4 byte header length ]
|
2000-07-25 19:53:58 +02:00
|
|
|
* 2: WORD flags
|
|
|
|
* 4: CHAR[len-4]
|
|
|
|
* (stringentry i of a subentry refers to the ID 'firstentry+i')
|
|
|
|
*
|
|
|
|
* Yes, ANSI strings in win32 resources. Go figure.
|
|
|
|
*/
|
|
|
|
|
2004-04-19 22:12:14 +02:00
|
|
|
static const WCHAR FMTWSTR[] = { '%','s',0 };
|
2004-03-19 02:52:10 +01:00
|
|
|
|
2000-07-25 19:53:58 +02:00
|
|
|
/**********************************************************************
|
2010-04-18 16:10:19 +02:00
|
|
|
* load_message (internal)
|
2000-07-25 19:53:58 +02:00
|
|
|
*/
|
2010-04-18 16:10:19 +02:00
|
|
|
static LPWSTR load_message( HMODULE module, UINT id, WORD lang )
|
2000-07-25 19:53:58 +02:00
|
|
|
{
|
2003-05-07 05:19:17 +02:00
|
|
|
const MESSAGE_RESOURCE_ENTRY *mre;
|
2004-07-21 00:12:21 +02:00
|
|
|
WCHAR *buffer;
|
2009-10-29 17:40:15 +01:00
|
|
|
NTSTATUS status;
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2004-07-21 00:12:21 +02:00
|
|
|
TRACE("module = %p, id = %08x\n", module, id );
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2004-07-21 00:12:21 +02:00
|
|
|
if (!module) module = GetModuleHandleW( NULL );
|
2009-10-29 17:40:15 +01:00
|
|
|
if ((status = RtlFindMessage( module, RT_MESSAGETABLE, lang, id, &mre )) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
SetLastError( RtlNtStatusToDosError(status) );
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2004-03-19 02:52:10 +01:00
|
|
|
if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
|
2004-07-21 00:12:21 +02:00
|
|
|
{
|
|
|
|
int len = (strlenW( (const WCHAR *)mre->Text ) + 1) * sizeof(WCHAR);
|
|
|
|
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
|
|
|
|
memcpy( buffer, mre->Text, len );
|
|
|
|
}
|
2004-03-19 02:52:10 +01:00
|
|
|
else
|
2004-07-21 00:12:21 +02:00
|
|
|
{
|
2005-08-17 12:28:35 +02:00
|
|
|
int len = MultiByteToWideChar( CP_ACP, 0, (const char *)mre->Text, -1, NULL, 0 );
|
2004-07-21 00:12:21 +02:00
|
|
|
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
|
2005-08-17 12:28:35 +02:00
|
|
|
MultiByteToWideChar( CP_ACP, 0, (const char*)mre->Text, -1, buffer, len );
|
2000-07-25 19:53:58 +02:00
|
|
|
}
|
2004-07-21 00:12:21 +02:00
|
|
|
TRACE("returning %s\n", wine_dbgstr_w(buffer));
|
|
|
|
return buffer;
|
2000-07-25 19:53:58 +02:00
|
|
|
}
|
|
|
|
|
2009-10-29 17:23:00 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* get_arg (internal)
|
|
|
|
*/
|
2009-10-29 19:11:28 +01:00
|
|
|
static ULONG_PTR get_arg( int nr, DWORD flags, struct format_args *args )
|
2009-10-29 17:23:00 +01:00
|
|
|
{
|
2009-10-29 19:11:28 +01:00
|
|
|
if (nr == -1) nr = args->last + 1;
|
|
|
|
if (args->list)
|
|
|
|
{
|
|
|
|
if (!args->args) args->args = HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR) );
|
|
|
|
while (nr > args->last)
|
|
|
|
args->args[args->last++] = va_arg( *args->list, ULONG_PTR );
|
|
|
|
}
|
|
|
|
if (nr > args->last) args->last = nr;
|
|
|
|
return args->args[nr - 1];
|
2009-10-29 17:23:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
2010-04-18 16:09:59 +02:00
|
|
|
* format_insert (internal)
|
2009-10-29 17:23:00 +01:00
|
|
|
*/
|
2010-04-18 16:09:59 +02:00
|
|
|
static LPCWSTR format_insert( BOOL unicode_caller, int insert, LPCWSTR format,
|
|
|
|
DWORD flags, struct format_args *args,
|
|
|
|
LPWSTR *result )
|
2009-10-29 17:23:00 +01:00
|
|
|
{
|
2010-04-18 16:09:53 +02:00
|
|
|
static const WCHAR fmt_lu[] = {'%','l','u',0};
|
|
|
|
WCHAR *wstring = NULL, *p, fmt[256];
|
2009-10-29 17:23:00 +01:00
|
|
|
ULONG_PTR arg;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (*format != '!') /* simple string */
|
|
|
|
{
|
2010-04-18 16:09:59 +02:00
|
|
|
arg = get_arg( insert, flags, args );
|
2012-01-06 19:48:44 +01:00
|
|
|
if (unicode_caller || !arg)
|
2009-10-29 17:23:00 +01:00
|
|
|
{
|
2012-01-06 19:48:44 +01:00
|
|
|
static const WCHAR nullW[] = {'(','n','u','l','l',')',0};
|
|
|
|
const WCHAR *str = (const WCHAR *)arg;
|
|
|
|
|
|
|
|
if (!str) str = nullW;
|
2010-04-18 16:09:59 +02:00
|
|
|
*result = HeapAlloc( GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR) );
|
|
|
|
strcpyW( *result, str );
|
2009-10-29 17:23:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-06 19:48:44 +01:00
|
|
|
const char *str = (const char *)arg;
|
2010-04-18 16:09:59 +02:00
|
|
|
DWORD length = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
|
|
|
|
*result = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
|
|
|
|
MultiByteToWideChar( CP_ACP, 0, str, -1, *result, length );
|
2009-10-29 17:23:00 +01:00
|
|
|
}
|
2009-10-29 17:36:45 +01:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
format++;
|
|
|
|
p = fmt;
|
|
|
|
*p++ = '%';
|
|
|
|
|
|
|
|
while (*format == '0' ||
|
|
|
|
*format == '+' ||
|
|
|
|
*format == '-' ||
|
|
|
|
*format == ' ' ||
|
|
|
|
*format == '*' ||
|
|
|
|
*format == '#')
|
|
|
|
{
|
|
|
|
if (*format == '*')
|
|
|
|
{
|
|
|
|
p += sprintfW( p, fmt_lu, get_arg( insert, flags, args ));
|
2009-10-29 19:11:28 +01:00
|
|
|
insert = -1;
|
2009-10-29 17:36:45 +01:00
|
|
|
format++;
|
|
|
|
}
|
|
|
|
else *p++ = *format++;
|
|
|
|
}
|
|
|
|
while (isdigitW(*format)) *p++ = *format++;
|
|
|
|
|
|
|
|
if (*format == '.')
|
|
|
|
{
|
|
|
|
*p++ = *format++;
|
|
|
|
if (*format == '*')
|
|
|
|
{
|
|
|
|
p += sprintfW( p, fmt_lu, get_arg( insert, flags, args ));
|
2009-10-29 19:11:28 +01:00
|
|
|
insert = -1;
|
2009-10-29 17:36:45 +01:00
|
|
|
format++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (isdigitW(*format)) *p++ = *format++;
|
|
|
|
}
|
|
|
|
|
2009-10-29 20:48:26 +01:00
|
|
|
/* replicate MS bug: drop an argument when using va_list with width/precision */
|
|
|
|
if (insert == -1 && args->list) args->last--;
|
2009-10-29 17:36:45 +01:00
|
|
|
arg = get_arg( insert, flags, args );
|
|
|
|
|
|
|
|
/* check for ascii string format */
|
|
|
|
if ((format[0] == 'h' && format[1] == 's') ||
|
|
|
|
(format[0] == 'h' && format[1] == 'S') ||
|
2010-04-18 16:09:59 +02:00
|
|
|
(unicode_caller && format[0] == 'S') ||
|
|
|
|
(!unicode_caller && format[0] == 's'))
|
2009-10-29 17:36:45 +01:00
|
|
|
{
|
2010-04-18 16:09:59 +02:00
|
|
|
DWORD len = MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, NULL, 0 );
|
2009-10-29 17:36:45 +01:00
|
|
|
wstring = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
|
|
|
|
MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, wstring, len );
|
|
|
|
arg = (ULONG_PTR)wstring;
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for ascii character format */
|
|
|
|
else if ((format[0] == 'h' && format[1] == 'c') ||
|
|
|
|
(format[0] == 'h' && format[1] == 'C') ||
|
2010-04-18 16:09:59 +02:00
|
|
|
(unicode_caller && format[0] == 'C') ||
|
|
|
|
(!unicode_caller && format[0] == 'c'))
|
2009-10-29 17:36:45 +01:00
|
|
|
{
|
|
|
|
char ch = arg;
|
2009-12-02 06:24:11 +01:00
|
|
|
wstring = HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR) );
|
2009-10-29 17:36:45 +01:00
|
|
|
MultiByteToWideChar( CP_ACP, 0, &ch, 1, wstring, 1 );
|
|
|
|
wstring[1] = 0;
|
|
|
|
arg = (ULONG_PTR)wstring;
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for wide string format */
|
|
|
|
else if ((format[0] == 'l' && format[1] == 's') ||
|
|
|
|
(format[0] == 'l' && format[1] == 'S') ||
|
2010-04-18 16:09:59 +02:00
|
|
|
(format[0] == 'w' && format[1] == 's') ||
|
|
|
|
(!unicode_caller && format[0] == 'S'))
|
2009-10-29 17:36:45 +01:00
|
|
|
{
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for wide character format */
|
|
|
|
else if ((format[0] == 'l' && format[1] == 'c') ||
|
|
|
|
(format[0] == 'l' && format[1] == 'C') ||
|
2010-04-18 16:09:59 +02:00
|
|
|
(format[0] == 'w' && format[1] == 'c') ||
|
|
|
|
(!unicode_caller && format[0] == 'C'))
|
2009-10-29 17:36:45 +01:00
|
|
|
{
|
|
|
|
*p++ = 'c';
|
|
|
|
}
|
|
|
|
/* FIXME: handle I64 etc. */
|
|
|
|
else while (*format && *format != '!') *p++ = *format++;
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
size = 256;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
|
|
|
|
int needed = snprintfW( ret, size, fmt, arg );
|
|
|
|
if (needed == -1 || needed >= size)
|
|
|
|
{
|
|
|
|
HeapFree( GetProcessHeap(), 0, ret );
|
|
|
|
size = max( needed + 1, size * 2 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*result = ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*format && *format != '!') format++;
|
|
|
|
if (*format == '!') format++;
|
|
|
|
|
|
|
|
HeapFree( GetProcessHeap(), 0, wstring );
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:37:24 +02:00
|
|
|
struct _format_message_data
|
|
|
|
{
|
|
|
|
LPWSTR formatted;
|
|
|
|
DWORD size;
|
|
|
|
LPWSTR t;
|
2012-09-24 08:38:13 +02:00
|
|
|
LPWSTR space;
|
|
|
|
BOOL inspace;
|
|
|
|
DWORD width, w;
|
2012-09-19 20:37:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void format_add_char(struct _format_message_data *fmd, WCHAR c)
|
|
|
|
{
|
|
|
|
*fmd->t++ = c;
|
2012-09-24 08:38:13 +02:00
|
|
|
if (fmd->width && fmd->width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
fmd->space = NULL;
|
|
|
|
fmd->inspace = FALSE;
|
|
|
|
fmd->w = 0;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
if (!fmd->inspace)
|
|
|
|
fmd->space = fmd->t - 1;
|
|
|
|
fmd->inspace = TRUE;
|
|
|
|
fmd->w++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fmd->inspace = FALSE;
|
|
|
|
fmd->w++;
|
|
|
|
}
|
|
|
|
if (fmd->w == fmd->width) {
|
|
|
|
LPWSTR notspace;
|
|
|
|
if (fmd->space) {
|
|
|
|
notspace = fmd->space;
|
|
|
|
while (*notspace == ' ' && notspace != fmd->t)
|
|
|
|
notspace++;
|
|
|
|
} else
|
|
|
|
notspace = fmd->space = fmd->t;
|
|
|
|
fmd->w = fmd->t - notspace;
|
|
|
|
memmove(fmd->space+2, notspace, fmd->w * sizeof(*fmd->t));
|
|
|
|
*fmd->space++ = '\r';
|
|
|
|
*fmd->space++ = '\n';
|
|
|
|
fmd->t = fmd->space + fmd->w;
|
|
|
|
fmd->space = NULL;
|
|
|
|
fmd->inspace = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 20:37:24 +02:00
|
|
|
if ((DWORD)(fmd->t - fmd->formatted) == fmd->size) {
|
2012-09-24 08:38:13 +02:00
|
|
|
DWORD_PTR ispace = fmd->space - fmd->formatted;
|
|
|
|
/* Allocate two extra characters so we can insert a '\r\n' in
|
|
|
|
* the middle of a word.
|
|
|
|
*/
|
|
|
|
fmd->formatted = HeapReAlloc(GetProcessHeap(), 0, fmd->formatted, (fmd->size * 2 + 2) * sizeof(WCHAR));
|
2012-09-19 20:37:24 +02:00
|
|
|
fmd->t = fmd->formatted + fmd->size;
|
2012-09-24 08:38:13 +02:00
|
|
|
if (fmd->space)
|
|
|
|
fmd->space = fmd->formatted + ispace;
|
2012-09-19 20:37:24 +02:00
|
|
|
fmd->size *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-18 16:09:45 +02:00
|
|
|
/**********************************************************************
|
2010-04-18 16:10:08 +02:00
|
|
|
* format_message (internal)
|
2010-04-18 16:09:45 +02:00
|
|
|
*/
|
2010-04-18 16:10:08 +02:00
|
|
|
static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr,
|
|
|
|
struct format_args *format_args )
|
2010-04-18 16:09:28 +02:00
|
|
|
{
|
2012-09-19 20:37:24 +02:00
|
|
|
struct _format_message_data fmd;
|
2010-04-18 16:09:28 +02:00
|
|
|
LPCWSTR f;
|
|
|
|
BOOL eos = FALSE;
|
|
|
|
|
2012-09-19 20:37:24 +02:00
|
|
|
fmd.size = 100;
|
2012-09-24 08:38:13 +02:00
|
|
|
fmd.formatted = fmd.t = HeapAlloc( GetProcessHeap(), 0, (fmd.size + 2) * sizeof(WCHAR) );
|
2010-04-18 16:09:28 +02:00
|
|
|
|
2012-09-24 08:38:13 +02:00
|
|
|
fmd.width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
|
|
|
|
fmd.w = 0;
|
|
|
|
fmd.inspace = FALSE;
|
|
|
|
fmd.space = NULL;
|
2010-04-18 16:09:28 +02:00
|
|
|
f = fmtstr;
|
2010-04-28 11:20:48 +02:00
|
|
|
while (*f && !eos) {
|
|
|
|
if (*f=='%') {
|
|
|
|
int insertnr;
|
|
|
|
WCHAR *str,*x;
|
|
|
|
|
|
|
|
f++;
|
|
|
|
switch (*f) {
|
|
|
|
case '1':case '2':case '3':case '4':case '5':
|
|
|
|
case '6':case '7':case '8':case '9':
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS)
|
|
|
|
goto ignore_inserts;
|
2010-05-01 10:40:46 +02:00
|
|
|
else if (((dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) && !format_args->args) ||
|
|
|
|
(!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) && !format_args->list))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2012-09-19 20:37:24 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, fmd.formatted);
|
2010-05-01 10:40:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-28 11:20:48 +02:00
|
|
|
insertnr = *f-'0';
|
|
|
|
switch (f[1]) {
|
|
|
|
case '0':case '1':case '2':case '3':
|
|
|
|
case '4':case '5':case '6':case '7':
|
|
|
|
case '8':case '9':
|
2010-04-28 11:20:35 +02:00
|
|
|
f++;
|
2010-04-28 11:20:48 +02:00
|
|
|
insertnr = insertnr*10 + *f-'0';
|
2010-04-28 11:20:35 +02:00
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
default:
|
2010-04-28 11:20:48 +02:00
|
|
|
f++;
|
2010-04-28 11:20:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-04-28 11:20:48 +02:00
|
|
|
f = format_insert( unicode_caller, insertnr, f, dwFlags, format_args, &str );
|
2012-09-19 20:37:24 +02:00
|
|
|
for (x = str; *x; x++) format_add_char(&fmd, *x);
|
2010-04-28 11:20:48 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, str );
|
|
|
|
break;
|
|
|
|
case 'n':
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, '\r');
|
|
|
|
format_add_char(&fmd, '\n');
|
2010-04-18 16:09:28 +02:00
|
|
|
f++;
|
2010-04-28 11:20:48 +02:00
|
|
|
break;
|
|
|
|
case 'r':
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, '\r');
|
2010-04-28 11:20:48 +02:00
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case 't':
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, '\t');
|
2010-04-28 11:20:48 +02:00
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
eos = TRUE;
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2012-09-19 20:37:24 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, fmd.formatted);
|
2010-04-28 11:20:48 +02:00
|
|
|
return NULL;
|
|
|
|
ignore_inserts:
|
|
|
|
default:
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS)
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, '%');
|
|
|
|
format_add_char(&fmd, *f++);
|
2010-04-28 11:20:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2012-09-19 20:37:24 +02:00
|
|
|
WCHAR ch = *f;
|
2010-04-28 11:20:48 +02:00
|
|
|
f++;
|
|
|
|
if (ch == '\r') {
|
|
|
|
if (*f == '\n')
|
2010-04-18 16:09:28 +02:00
|
|
|
f++;
|
2012-09-24 08:38:13 +02:00
|
|
|
if(fmd.width)
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, ' ');
|
2010-04-28 11:20:48 +02:00
|
|
|
else
|
|
|
|
{
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, '\r');
|
|
|
|
format_add_char(&fmd, '\n');
|
2010-04-18 16:09:28 +02:00
|
|
|
}
|
|
|
|
} else {
|
2010-04-28 11:20:48 +02:00
|
|
|
if (ch == '\n')
|
|
|
|
{
|
2012-09-24 08:38:13 +02:00
|
|
|
if(fmd.width)
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, ' ');
|
2010-04-18 16:09:28 +02:00
|
|
|
else
|
|
|
|
{
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, '\r');
|
|
|
|
format_add_char(&fmd, '\n');
|
2010-04-18 16:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-28 11:20:48 +02:00
|
|
|
else
|
2012-09-19 20:37:24 +02:00
|
|
|
format_add_char(&fmd, ch);
|
2010-04-18 16:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 20:37:24 +02:00
|
|
|
*fmd.t = '\0';
|
2010-04-18 16:09:28 +02:00
|
|
|
|
2012-09-19 20:37:24 +02:00
|
|
|
return fmd.formatted;
|
2010-04-18 16:09:28 +02:00
|
|
|
}
|
2009-10-29 17:36:45 +01:00
|
|
|
|
2000-07-25 19:53:58 +02:00
|
|
|
/***********************************************************************
|
2001-06-13 22:13:18 +02:00
|
|
|
* FormatMessageA (KERNEL32.@)
|
2000-07-25 19:53:58 +02:00
|
|
|
*/
|
|
|
|
DWORD WINAPI FormatMessageA(
|
|
|
|
DWORD dwFlags,
|
|
|
|
LPCVOID lpSource,
|
|
|
|
DWORD dwMessageId,
|
|
|
|
DWORD dwLanguageId,
|
|
|
|
LPSTR lpBuffer,
|
|
|
|
DWORD nSize,
|
2009-10-29 17:23:00 +01:00
|
|
|
__ms_va_list* args )
|
2001-05-09 21:43:51 +02:00
|
|
|
{
|
2009-10-29 19:11:28 +01:00
|
|
|
struct format_args format_args;
|
2006-06-13 14:10:39 +02:00
|
|
|
DWORD ret = 0;
|
2010-04-18 16:09:53 +02:00
|
|
|
LPWSTR target;
|
|
|
|
DWORD destlength;
|
|
|
|
LPWSTR from;
|
2001-05-09 21:43:51 +02:00
|
|
|
|
2006-10-12 22:56:29 +02:00
|
|
|
TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
|
2001-05-09 21:43:51 +02:00
|
|
|
dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
|
|
|
|
|
2010-04-28 11:20:25 +02:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
|
2007-09-26 14:09:46 +02:00
|
|
|
{
|
2010-04-28 11:20:25 +02:00
|
|
|
if (!lpBuffer)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*(LPSTR *)lpBuffer = NULL;
|
2007-09-26 14:09:46 +02:00
|
|
|
}
|
|
|
|
|
2009-10-29 19:11:28 +01:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
|
|
|
|
{
|
|
|
|
format_args.args = (ULONG_PTR *)args;
|
|
|
|
format_args.list = NULL;
|
|
|
|
format_args.last = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_args.args = NULL;
|
|
|
|
format_args.list = args;
|
|
|
|
format_args.last = 0;
|
|
|
|
}
|
|
|
|
|
2001-05-09 21:43:51 +02:00
|
|
|
from = NULL;
|
2001-07-24 23:45:22 +02:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
|
|
|
|
{
|
2010-04-18 16:09:53 +02:00
|
|
|
DWORD length = MultiByteToWideChar(CP_ACP, 0, lpSource, -1, NULL, 0);
|
|
|
|
from = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, lpSource, -1, from, length);
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2010-04-28 11:20:25 +02:00
|
|
|
else if (dwFlags & (FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM))
|
|
|
|
{
|
2002-11-05 00:49:53 +01:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
|
2010-04-18 16:10:19 +02:00
|
|
|
from = load_message( (HMODULE)lpSource, dwMessageId, dwLanguageId );
|
2004-07-21 00:12:21 +02:00
|
|
|
if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
|
2010-04-18 16:10:19 +02:00
|
|
|
from = load_message( kernel32_handle, dwMessageId, dwLanguageId );
|
2009-10-29 17:40:15 +01:00
|
|
|
if (!from) return 0;
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2010-04-28 11:20:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2010-04-18 16:10:08 +02:00
|
|
|
target = format_message( FALSE, dwFlags, from, &format_args );
|
2010-04-28 11:20:19 +02:00
|
|
|
if (!target)
|
|
|
|
goto failure;
|
2001-05-09 21:43:51 +02:00
|
|
|
|
2010-04-18 16:09:53 +02:00
|
|
|
TRACE("-- %s\n", debugstr_w(target));
|
2010-04-28 11:20:32 +02:00
|
|
|
|
|
|
|
/* Only try writing to an output buffer if there are processed characters
|
|
|
|
* in the temporary output buffer. */
|
|
|
|
if (*target)
|
|
|
|
{
|
|
|
|
destlength = WideCharToMultiByte(CP_ACP, 0, target, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
|
2010-04-18 16:08:54 +02:00
|
|
|
{
|
2010-04-28 11:20:32 +02:00
|
|
|
LPSTR buf = LocalAlloc(LMEM_ZEROINIT, max(nSize, destlength));
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, target, -1, buf, destlength, NULL, NULL);
|
|
|
|
*((LPSTR*)lpBuffer) = buf;
|
2010-04-18 16:08:54 +02:00
|
|
|
}
|
2010-04-28 11:20:32 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nSize < destlength)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, target, -1, lpBuffer, destlength, NULL, NULL);
|
|
|
|
}
|
|
|
|
ret = destlength - 1; /* null terminator */
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2010-04-18 16:08:54 +02:00
|
|
|
|
|
|
|
failure:
|
2001-05-09 21:43:51 +02:00
|
|
|
HeapFree(GetProcessHeap(),0,target);
|
2004-12-23 18:06:43 +01:00
|
|
|
HeapFree(GetProcessHeap(),0,from);
|
2009-10-29 19:11:28 +01:00
|
|
|
if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
|
2010-04-18 16:08:54 +02:00
|
|
|
TRACE("-- returning %u\n", ret);
|
2006-06-13 14:10:39 +02:00
|
|
|
return ret;
|
2000-07-25 19:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2001-06-13 22:13:18 +02:00
|
|
|
* FormatMessageW (KERNEL32.@)
|
2000-07-25 19:53:58 +02:00
|
|
|
*/
|
|
|
|
DWORD WINAPI FormatMessageW(
|
|
|
|
DWORD dwFlags,
|
|
|
|
LPCVOID lpSource,
|
|
|
|
DWORD dwMessageId,
|
|
|
|
DWORD dwLanguageId,
|
|
|
|
LPWSTR lpBuffer,
|
|
|
|
DWORD nSize,
|
2009-10-29 17:36:45 +01:00
|
|
|
__ms_va_list* args )
|
2001-05-09 21:43:51 +02:00
|
|
|
{
|
2009-10-29 19:11:28 +01:00
|
|
|
struct format_args format_args;
|
2010-04-18 16:08:54 +02:00
|
|
|
DWORD ret = 0;
|
2010-04-18 16:09:28 +02:00
|
|
|
LPWSTR target;
|
2001-05-09 21:43:51 +02:00
|
|
|
DWORD talloced;
|
2009-10-29 17:36:45 +01:00
|
|
|
LPWSTR from;
|
2001-05-09 21:43:51 +02:00
|
|
|
|
2006-10-12 22:56:29 +02:00
|
|
|
TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
|
2001-05-09 21:43:51 +02:00
|
|
|
dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
|
|
|
|
|
2007-09-26 14:09:46 +02:00
|
|
|
if (!lpBuffer)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-28 11:20:25 +02:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
|
|
|
|
*(LPWSTR *)lpBuffer = NULL;
|
|
|
|
|
2009-10-29 19:11:28 +01:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
|
|
|
|
{
|
|
|
|
format_args.args = (ULONG_PTR *)args;
|
|
|
|
format_args.list = NULL;
|
|
|
|
format_args.last = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_args.args = NULL;
|
|
|
|
format_args.list = args;
|
|
|
|
format_args.last = 0;
|
|
|
|
}
|
|
|
|
|
2001-05-09 21:43:51 +02:00
|
|
|
from = NULL;
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
|
2009-03-11 09:39:16 +01:00
|
|
|
from = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource) + 1) *
|
2004-03-19 02:52:10 +01:00
|
|
|
sizeof(WCHAR) );
|
2009-03-11 09:39:16 +01:00
|
|
|
strcpyW( from, lpSource );
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2010-04-28 11:20:25 +02:00
|
|
|
else if (dwFlags & (FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM))
|
|
|
|
{
|
2002-11-05 00:49:53 +01:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
|
2010-04-18 16:10:19 +02:00
|
|
|
from = load_message( (HMODULE)lpSource, dwMessageId, dwLanguageId );
|
2004-07-21 00:12:21 +02:00
|
|
|
if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
|
2010-04-18 16:10:19 +02:00
|
|
|
from = load_message( kernel32_handle, dwMessageId, dwLanguageId );
|
2009-10-29 17:40:15 +01:00
|
|
|
if (!from) return 0;
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2010-04-28 11:20:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-09 21:43:51 +02:00
|
|
|
|
2010-04-18 16:10:08 +02:00
|
|
|
target = format_message( TRUE, dwFlags, from, &format_args );
|
2010-04-28 11:20:19 +02:00
|
|
|
if (!target)
|
|
|
|
goto failure;
|
2001-05-09 21:43:51 +02:00
|
|
|
|
2004-03-19 02:52:10 +01:00
|
|
|
talloced = strlenW(target)+1;
|
2010-04-18 16:08:54 +02:00
|
|
|
TRACE("-- %s\n",debugstr_w(target));
|
2010-04-28 11:20:32 +02:00
|
|
|
|
|
|
|
/* Only allocate a buffer if there are processed characters in the
|
|
|
|
* temporary output buffer. If a caller supplies the buffer, then
|
|
|
|
* a null terminator will be written to it. */
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
|
|
|
|
{
|
|
|
|
if (*target)
|
|
|
|
{
|
|
|
|
/* nSize is the MINIMUM size */
|
|
|
|
*((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT, max(nSize, talloced)*sizeof(WCHAR));
|
|
|
|
strcpyW(*(LPWSTR*)lpBuffer, target);
|
|
|
|
}
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2010-04-18 16:08:54 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nSize < talloced)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
strcpyW(lpBuffer, target);
|
|
|
|
}
|
2004-03-19 02:52:10 +01:00
|
|
|
|
2010-04-18 16:08:54 +02:00
|
|
|
ret = talloced - 1; /* null terminator */
|
|
|
|
failure:
|
2001-05-09 21:43:51 +02:00
|
|
|
HeapFree(GetProcessHeap(),0,target);
|
2004-12-23 18:06:43 +01:00
|
|
|
HeapFree(GetProcessHeap(),0,from);
|
2009-10-29 19:11:28 +01:00
|
|
|
if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
|
2010-04-18 16:08:54 +02:00
|
|
|
TRACE("-- returning %u\n", ret);
|
|
|
|
return ret;
|
2000-07-25 19:53:58 +02:00
|
|
|
}
|