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 PCNTFMTWSTR[] = { '%','%','%','s',0 };
|
|
|
|
static const WCHAR FMTWSTR[] = { '%','s',0 };
|
2004-03-19 02:52:10 +01:00
|
|
|
|
2000-07-25 19:53:58 +02:00
|
|
|
/**********************************************************************
|
2004-03-19 02:52:10 +01:00
|
|
|
* load_messageW (internal)
|
2000-07-25 19:53:58 +02:00
|
|
|
*/
|
2004-07-21 00:12:21 +02:00
|
|
|
static LPWSTR load_messageW( 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
|
|
|
}
|
|
|
|
|
2004-03-19 02:52:10 +01:00
|
|
|
|
2000-07-25 19:53:58 +02:00
|
|
|
/**********************************************************************
|
2004-03-19 02:52:10 +01:00
|
|
|
* load_messageA (internal)
|
2000-07-25 19:53:58 +02:00
|
|
|
*/
|
2004-07-21 00:12:21 +02:00
|
|
|
static LPSTR load_messageA( HMODULE module, UINT id, WORD lang )
|
2000-07-25 19:53:58 +02:00
|
|
|
{
|
2004-07-21 00:12:21 +02:00
|
|
|
const MESSAGE_RESOURCE_ENTRY *mre;
|
|
|
|
char *buffer;
|
2009-10-29 17:40:15 +01:00
|
|
|
NTSTATUS status;
|
2004-03-19 02:52:10 +01:00
|
|
|
|
2004-07-21 00:12:21 +02:00
|
|
|
TRACE("module = %p, id = %08x\n", module, id );
|
2004-03-19 02:52:10 +01: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;
|
|
|
|
}
|
2004-03-19 02:52:10 +01:00
|
|
|
|
2004-07-21 00:12:21 +02:00
|
|
|
if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
|
2000-07-25 19:53:58 +02:00
|
|
|
{
|
2004-07-21 00:12:21 +02:00
|
|
|
int len = WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, NULL, 0, NULL, NULL );
|
|
|
|
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
|
|
|
|
WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, buffer, len, NULL, NULL );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-08-17 12:28:35 +02:00
|
|
|
int len = strlen((const char*)mre->Text) + 1;
|
2004-07-21 00:12:21 +02:00
|
|
|
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
|
|
|
|
memcpy( buffer, mre->Text, len );
|
|
|
|
}
|
|
|
|
TRACE("returning %s\n", wine_dbgstr_a(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* format_insertA (internal)
|
|
|
|
*/
|
2009-10-29 19:11:28 +01:00
|
|
|
static LPCSTR format_insertA( int insert, LPCSTR format, DWORD flags,
|
|
|
|
struct format_args *args, LPSTR *result )
|
2009-10-29 17:23:00 +01:00
|
|
|
{
|
|
|
|
char *astring = NULL, *p, fmt[256];
|
|
|
|
ULONG_PTR arg;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (*format != '!') /* simple string */
|
|
|
|
{
|
|
|
|
char *str = (char *)get_arg( insert, flags, args );
|
|
|
|
*result = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
|
|
|
|
strcpy( *result, str );
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
format++;
|
|
|
|
p = fmt;
|
|
|
|
*p++ = '%';
|
|
|
|
|
|
|
|
while (*format == '0' ||
|
|
|
|
*format == '+' ||
|
|
|
|
*format == '-' ||
|
|
|
|
*format == ' ' ||
|
|
|
|
*format == '*' ||
|
|
|
|
*format == '#')
|
|
|
|
{
|
|
|
|
if (*format == '*')
|
|
|
|
{
|
|
|
|
p += sprintf( p, "%lu", get_arg( insert, flags, args ));
|
2009-10-29 19:11:28 +01:00
|
|
|
insert = -1;
|
2009-10-29 17:23:00 +01:00
|
|
|
format++;
|
|
|
|
}
|
|
|
|
else *p++ = *format++;
|
|
|
|
}
|
|
|
|
while (isdigit(*format)) *p++ = *format++;
|
|
|
|
|
|
|
|
if (*format == '.')
|
|
|
|
{
|
|
|
|
*p++ = *format++;
|
|
|
|
if (*format == '*')
|
|
|
|
{
|
|
|
|
p += sprintf( p, "%lu", get_arg( insert, flags, args ));
|
2009-10-29 19:11:28 +01:00
|
|
|
insert = -1;
|
2009-10-29 17:23:00 +01:00
|
|
|
format++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (isdigit(*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:23:00 +01:00
|
|
|
arg = get_arg( insert, flags, args );
|
|
|
|
|
|
|
|
/* check for wide string format */
|
|
|
|
if ((format[0] == 'l' && format[1] == 's') ||
|
|
|
|
(format[0] == 'l' && format[1] == 'S') ||
|
|
|
|
(format[0] == 'w' && format[1] == 's') ||
|
|
|
|
(format[0] == 'S'))
|
|
|
|
{
|
|
|
|
DWORD len = WideCharToMultiByte( CP_ACP, 0, (WCHAR *)arg, -1, /*FIXME*/
|
|
|
|
NULL, 0, NULL, NULL );
|
|
|
|
astring = HeapAlloc( GetProcessHeap(), 0, len );
|
|
|
|
WideCharToMultiByte( CP_ACP, 0, (WCHAR *)arg, -1, astring, len, NULL, NULL );
|
|
|
|
arg = (ULONG_PTR)astring;
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for wide character format */
|
|
|
|
else if ((format[0] == 'l' && format[1] == 'c') ||
|
|
|
|
(format[0] == 'l' && format[1] == 'C') ||
|
|
|
|
(format[0] == 'w' && format[1] == 'c') ||
|
|
|
|
(format[0] == 'C'))
|
|
|
|
{
|
|
|
|
WCHAR ch = arg;
|
|
|
|
DWORD len = WideCharToMultiByte( CP_ACP, 0, &ch, 1, NULL, 0, NULL, NULL );
|
|
|
|
astring = HeapAlloc( GetProcessHeap(), 0, len + 1 );
|
|
|
|
WideCharToMultiByte( CP_ACP, 0, &ch, 1, astring, len, NULL, NULL );
|
|
|
|
astring[len] = 0;
|
|
|
|
arg = (ULONG_PTR)astring;
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for ascii string format */
|
|
|
|
else if ((format[0] == 'h' && format[1] == 's') ||
|
|
|
|
(format[0] == 'h' && format[1] == 'S'))
|
|
|
|
{
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for ascii character format */
|
|
|
|
else if ((format[0] == 'h' && format[1] == 'c') ||
|
|
|
|
(format[0] == 'h' && format[1] == 'C'))
|
|
|
|
{
|
|
|
|
*p++ = 'c';
|
|
|
|
}
|
|
|
|
/* FIXME: handle I64 etc. */
|
|
|
|
else while (*format && *format != '!') *p++ = *format++;
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
size = 256;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char *ret = HeapAlloc( GetProcessHeap(), 0, size );
|
|
|
|
int needed = snprintf( 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, astring );
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-29 17:36:45 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* format_insertW (internal)
|
|
|
|
*/
|
2009-10-29 19:11:28 +01:00
|
|
|
static LPCWSTR format_insertW( int insert, LPCWSTR format, DWORD flags,
|
|
|
|
struct format_args *args, LPWSTR *result )
|
2009-10-29 17:36:45 +01:00
|
|
|
{
|
|
|
|
static const WCHAR fmt_lu[] = {'%','l','u',0};
|
|
|
|
WCHAR *wstring = NULL, *p, fmt[256];
|
|
|
|
ULONG_PTR arg;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (*format != '!') /* simple string */
|
|
|
|
{
|
|
|
|
WCHAR *str = (WCHAR *)get_arg( insert, flags, args );
|
|
|
|
*result = HeapAlloc( GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR) );
|
|
|
|
strcpyW( *result, str );
|
|
|
|
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') ||
|
|
|
|
(format[0] == 'S'))
|
|
|
|
{
|
|
|
|
DWORD len = MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, /*FIXME*/ NULL, 0 );
|
|
|
|
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') ||
|
|
|
|
(format[0] == 'C'))
|
|
|
|
{
|
|
|
|
char ch = arg;
|
|
|
|
wstring = HeapAlloc( GetProcessHeap(), 0, 2 );
|
|
|
|
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') ||
|
|
|
|
(format[0] == 'w' && format[1] == 's'))
|
|
|
|
{
|
|
|
|
*p++ = 's';
|
|
|
|
}
|
|
|
|
/* check for wide character format */
|
|
|
|
else if ((format[0] == 'l' && format[1] == 'c') ||
|
|
|
|
(format[0] == 'l' && format[1] == 'C') ||
|
|
|
|
(format[0] == 'w' && format[1] == 'c'))
|
|
|
|
{
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
* FIXME: missing wrap,
|
|
|
|
*/
|
|
|
|
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;
|
2001-05-09 21:43:51 +02:00
|
|
|
LPSTR target,t;
|
|
|
|
DWORD talloced;
|
2009-10-29 17:23:00 +01:00
|
|
|
LPSTR from;
|
|
|
|
LPCSTR f;
|
2001-05-09 21:43:51 +02:00
|
|
|
DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
|
|
|
|
BOOL eos = FALSE;
|
|
|
|
CHAR ch;
|
|
|
|
|
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);
|
|
|
|
if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
|
|
|
|
&&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
|
|
|
|
|| (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
|
|
|
|
|
2007-09-26 14:09:46 +02:00
|
|
|
if (!lpBuffer)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
|
2006-10-12 22:56:29 +02:00
|
|
|
FIXME("line wrapping (%u) not supported.\n", width);
|
2001-05-09 21:43:51 +02:00
|
|
|
from = NULL;
|
2001-07-24 23:45:22 +02:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
|
|
|
|
{
|
2009-03-11 09:39:16 +01:00
|
|
|
from = HeapAlloc( GetProcessHeap(), 0, strlen(lpSource) + 1 );
|
|
|
|
strcpy( from, lpSource );
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2004-07-21 00:12:21 +02:00
|
|
|
from = NULL;
|
2002-11-05 00:49:53 +01:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
|
2004-07-21 00:12:21 +02:00
|
|
|
from = load_messageA( (HMODULE)lpSource, dwMessageId, dwLanguageId );
|
|
|
|
if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
|
|
|
|
from = load_messageA( kernel32_handle, dwMessageId, dwLanguageId );
|
2009-10-29 17:40:15 +01:00
|
|
|
if (!from) return 0;
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
|
|
|
target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
|
|
|
|
t = target;
|
|
|
|
talloced= 100;
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2000-07-31 22:56:52 +02:00
|
|
|
#define ADD_TO_T(c) do { \
|
2001-05-09 21:43:51 +02:00
|
|
|
*t++=c;\
|
2006-07-28 00:00:06 +02:00
|
|
|
if ((DWORD)(t-target) == talloced) {\
|
2005-03-24 22:01:35 +01:00
|
|
|
target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
|
2001-05-09 21:43:51 +02:00
|
|
|
t = target+talloced;\
|
|
|
|
talloced*=2;\
|
|
|
|
}\
|
2000-07-31 22:56:52 +02:00
|
|
|
} while (0)
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2001-05-09 21:43:51 +02:00
|
|
|
if (from) {
|
|
|
|
f=from;
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
|
|
|
|
while (*f && !eos)
|
|
|
|
ADD_TO_T(*f++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (*f && !eos) {
|
|
|
|
if (*f=='%') {
|
|
|
|
int insertnr;
|
2009-10-29 17:23:00 +01:00
|
|
|
char *str,*x;
|
2001-05-09 21:43:51 +02:00
|
|
|
|
|
|
|
f++;
|
|
|
|
if (!*f) {
|
|
|
|
ADD_TO_T('%');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (*f) {
|
|
|
|
case '1':case '2':case '3':case '4':case '5':
|
|
|
|
case '6':case '7':case '8':case '9':
|
|
|
|
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':
|
|
|
|
f++;
|
|
|
|
insertnr=insertnr*10+*f-'0';
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
}
|
2009-10-29 19:11:28 +01:00
|
|
|
f = format_insertA( insertnr, f, dwFlags, &format_args, &str );
|
2009-10-29 17:23:00 +01:00
|
|
|
for (x = str; *x; x++) ADD_TO_T(*x);
|
|
|
|
HeapFree( GetProcessHeap(), 0, str );
|
2001-05-09 21:43:51 +02:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
ADD_TO_T('\r');
|
|
|
|
ADD_TO_T('\n');
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
eos = TRUE;
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ADD_TO_T(*f++);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ch = *f;
|
|
|
|
f++;
|
|
|
|
if (ch == '\r') {
|
|
|
|
if (*f == '\n')
|
|
|
|
f++;
|
2002-08-20 02:32:48 +02:00
|
|
|
if(width)
|
|
|
|
ADD_TO_T(' ');
|
|
|
|
else
|
2001-05-09 21:43:51 +02:00
|
|
|
{
|
|
|
|
ADD_TO_T('\r');
|
|
|
|
ADD_TO_T('\n');
|
|
|
|
}
|
2002-08-20 02:32:48 +02:00
|
|
|
} else {
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
if(width)
|
|
|
|
ADD_TO_T(' ');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ADD_TO_T('\r');
|
|
|
|
ADD_TO_T('\n');
|
|
|
|
}
|
|
|
|
}
|
2001-05-09 21:43:51 +02:00
|
|
|
else
|
|
|
|
ADD_TO_T(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*t='\0';
|
|
|
|
}
|
|
|
|
talloced = strlen(target)+1;
|
|
|
|
if (nSize && talloced<nSize) {
|
2005-03-24 22:01:35 +01:00
|
|
|
target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
|
|
|
TRACE("-- %s\n",debugstr_a(target));
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
|
2007-12-13 23:29:37 +01:00
|
|
|
*((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT,max(nSize, talloced));
|
2001-05-09 21:43:51 +02:00
|
|
|
memcpy(*(LPSTR*)lpBuffer,target,talloced);
|
|
|
|
} else {
|
|
|
|
lstrcpynA(lpBuffer,target,nSize);
|
|
|
|
}
|
|
|
|
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 );
|
2006-06-13 14:10:39 +02:00
|
|
|
ret = (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer) : strlen(lpBuffer);
|
2006-10-12 22:56:29 +02:00
|
|
|
TRACE("-- returning %d\n", ret);
|
2006-06-13 14:10:39 +02:00
|
|
|
return ret;
|
2000-07-25 19:53:58 +02:00
|
|
|
}
|
|
|
|
#undef ADD_TO_T
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
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;
|
2004-03-19 02:52:10 +01:00
|
|
|
LPWSTR target,t;
|
2001-05-09 21:43:51 +02:00
|
|
|
DWORD talloced;
|
2009-10-29 17:36:45 +01:00
|
|
|
LPWSTR from;
|
|
|
|
LPCWSTR f;
|
2001-05-09 21:43:51 +02:00
|
|
|
DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
|
|
|
|
BOOL eos = FALSE;
|
2006-05-30 14:13:51 +02:00
|
|
|
WCHAR ch;
|
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);
|
|
|
|
if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
|
|
|
|
&&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
|
|
|
|
|| (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
|
|
|
|
|
2007-09-26 14:09:46 +02:00
|
|
|
if (!lpBuffer)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
|
|
|
|
FIXME("line wrapping not supported.\n");
|
|
|
|
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
|
|
|
}
|
|
|
|
else {
|
2004-07-21 00:12:21 +02:00
|
|
|
from = NULL;
|
2002-11-05 00:49:53 +01:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
|
2004-07-21 00:12:21 +02:00
|
|
|
from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
|
|
|
|
if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
|
|
|
|
from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
|
2009-10-29 17:40:15 +01:00
|
|
|
if (!from) return 0;
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2004-03-19 02:52:10 +01:00
|
|
|
target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) );
|
2001-05-09 21:43:51 +02:00
|
|
|
t = target;
|
|
|
|
talloced= 100;
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2000-07-31 22:56:52 +02:00
|
|
|
#define ADD_TO_T(c) do {\
|
2001-05-09 21:43:51 +02:00
|
|
|
*t++=c;\
|
2006-07-28 00:00:06 +02:00
|
|
|
if ((DWORD)(t-target) == talloced) {\
|
2005-03-24 22:01:35 +01:00
|
|
|
target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
|
2001-05-09 21:43:51 +02:00
|
|
|
t = target+talloced;\
|
|
|
|
talloced*=2;\
|
|
|
|
} \
|
2000-07-31 22:56:52 +02:00
|
|
|
} while (0)
|
2000-07-25 19:53:58 +02:00
|
|
|
|
2001-05-09 21:43:51 +02:00
|
|
|
if (from) {
|
|
|
|
f=from;
|
|
|
|
if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
|
|
|
|
while (*f && !eos)
|
|
|
|
ADD_TO_T(*f++);
|
2000-11-28 23:40:56 +01:00
|
|
|
}
|
2001-05-09 21:43:51 +02:00
|
|
|
else {
|
|
|
|
while (*f && !eos) {
|
|
|
|
if (*f=='%') {
|
|
|
|
int insertnr;
|
2009-10-29 17:36:45 +01:00
|
|
|
WCHAR *str,*x;
|
2001-05-09 21:43:51 +02:00
|
|
|
|
|
|
|
f++;
|
|
|
|
if (!*f) {
|
|
|
|
ADD_TO_T('%');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*f) {
|
|
|
|
case '1':case '2':case '3':case '4':case '5':
|
|
|
|
case '6':case '7':case '8':case '9':
|
|
|
|
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':
|
|
|
|
f++;
|
|
|
|
insertnr=insertnr*10+*f-'0';
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
}
|
2009-10-29 19:11:28 +01:00
|
|
|
f = format_insertW( insertnr, f, dwFlags, &format_args, &str );
|
2009-10-29 17:36:45 +01:00
|
|
|
for (x = str; *x; x++) ADD_TO_T(*x);
|
|
|
|
HeapFree( GetProcessHeap(), 0, str );
|
2001-05-09 21:43:51 +02:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
ADD_TO_T('\r');
|
|
|
|
ADD_TO_T('\n');
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
eos = TRUE;
|
|
|
|
f++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ADD_TO_T(*f++);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ch = *f;
|
|
|
|
f++;
|
|
|
|
if (ch == '\r') {
|
|
|
|
if (*f == '\n')
|
|
|
|
f++;
|
2002-08-20 02:32:48 +02:00
|
|
|
if(width)
|
|
|
|
ADD_TO_T(' ');
|
|
|
|
else
|
2001-05-09 21:43:51 +02:00
|
|
|
{
|
|
|
|
ADD_TO_T('\r');
|
|
|
|
ADD_TO_T('\n');
|
|
|
|
}
|
2002-08-20 02:32:48 +02:00
|
|
|
} else {
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
if(width)
|
|
|
|
ADD_TO_T(' ');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ADD_TO_T('\r');
|
|
|
|
ADD_TO_T('\n');
|
|
|
|
}
|
|
|
|
}
|
2001-05-09 21:43:51 +02:00
|
|
|
else
|
|
|
|
ADD_TO_T(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-11-28 23:40:56 +01:00
|
|
|
}
|
2001-05-09 21:43:51 +02:00
|
|
|
*t='\0';
|
|
|
|
}
|
2004-03-19 02:52:10 +01:00
|
|
|
talloced = strlenW(target)+1;
|
2001-05-09 21:43:51 +02:00
|
|
|
if (nSize && talloced<nSize)
|
2005-03-24 22:01:35 +01:00
|
|
|
target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize*sizeof(WCHAR));
|
2001-05-09 21:43:51 +02:00
|
|
|
if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
|
|
|
|
/* nSize is the MINIMUM size */
|
2004-03-19 02:52:10 +01:00
|
|
|
DWORD len = strlenW(target) + 1;
|
2007-12-13 23:29:37 +01:00
|
|
|
*((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT,len*sizeof(WCHAR));
|
2004-03-19 02:52:10 +01:00
|
|
|
strcpyW(*(LPWSTR*)lpBuffer, target);
|
2001-05-09 21:43:51 +02:00
|
|
|
}
|
2004-03-19 02:52:10 +01:00
|
|
|
else lstrcpynW(lpBuffer, target, nSize);
|
|
|
|
|
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 );
|
2004-03-19 02:52:10 +01:00
|
|
|
TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
|
|
|
|
*(LPWSTR*)lpBuffer : lpBuffer));
|
2001-05-09 21:43:51 +02:00
|
|
|
return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
|
|
|
|
strlenW(*(LPWSTR*)lpBuffer):
|
|
|
|
strlenW(lpBuffer);
|
2000-07-25 19:53:58 +02:00
|
|
|
}
|
|
|
|
#undef ADD_TO_T
|