2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "wine/unicode.h"
35 #include "kernel_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
41 /* Messages...used by FormatMessage (KERNEL32.something)
43 * They can be specified either directly or using a message ID and
44 * loading them from the resource.
46 * The resourcedata has following format:
48 * 0: DWORD nrofentries
49 * nrofentries * subentry:
52 * 8: DWORD offset from start to the stringentries
54 * (lastentry-firstentry) * stringentry:
55 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
58 * (stringentry i of a subentry refers to the ID 'firstentry+i')
60 * Yes, ANSI strings in win32 resources. Go figure.
63 static const WCHAR PCNTFMTWSTR
[] = { '%','%','%','s',0 };
64 static const WCHAR FMTWSTR
[] = { '%','s',0 };
66 /**********************************************************************
67 * load_messageW (internal)
69 static LPWSTR
load_messageW( HMODULE module
, UINT id
, WORD lang
)
71 const MESSAGE_RESOURCE_ENTRY
*mre
;
74 TRACE("module = %p, id = %08x\n", module
, id
);
76 if (!module
) module
= GetModuleHandleW( NULL
);
77 if (RtlFindMessage( module
, RT_MESSAGETABLE
, lang
, id
, &mre
) != STATUS_SUCCESS
) return NULL
;
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
));
96 /**********************************************************************
97 * load_messageA (internal)
99 static LPSTR
load_messageA( HMODULE module
, UINT id
, WORD lang
)
101 const MESSAGE_RESOURCE_ENTRY
*mre
;
104 TRACE("module = %p, id = %08x\n", module
, id
);
106 if (!module
) module
= GetModuleHandleW( NULL
);
107 if (RtlFindMessage( module
, RT_MESSAGETABLE
, lang
, id
, &mre
) != STATUS_SUCCESS
) return NULL
;
109 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
111 int len
= WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, NULL
, 0, NULL
, NULL
);
112 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
113 WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, buffer
, len
, NULL
, NULL
);
117 int len
= strlen((const char*)mre
->Text
) + 1;
118 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
119 memcpy( buffer
, mre
->Text
, len
);
121 TRACE("returning %s\n", wine_dbgstr_a(buffer
));
126 /***********************************************************************
127 * FormatMessageA (KERNEL32.@)
128 * FIXME: missing wrap,
130 DWORD WINAPI
FormatMessageA(
139 LPDWORD args
=(LPDWORD
)_args
;
140 #if defined(__i386__) || defined(__sparc__)
141 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
145 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
149 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
150 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
151 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
152 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
153 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
155 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
156 FIXME("line wrapping (%lu) not supported.\n", width
);
158 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
160 from
= HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR
)lpSource
)+1 );
161 strcpy( from
, (LPCSTR
)lpSource
);
165 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
166 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
167 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
168 from
= load_messageA( kernel32_handle
, dwMessageId
, dwLanguageId
);
172 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
176 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
180 #define ADD_TO_T(c) do { \
182 if (t-target == talloced) {\
183 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
184 t = target+talloced;\
191 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
199 char *fmtstr
,*x
,*lastf
;
210 case '1':case '2':case '3':case '4':case '5':
211 case '6':case '7':case '8':case '9':
214 case '0':case '1':case '2':case '3':
215 case '4':case '5':case '6':case '7':
218 insertnr
=insertnr
*10+*f
-'0';
227 if (NULL
!=(x
=strchr(f
,'!'))) {
229 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
230 sprintf(fmtstr
,"%%%s",f
);
233 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
234 sprintf(fmtstr
,"%%%s",f
);
235 f
+=strlen(f
); /*at \0*/
239 fmtstr
= HeapAlloc(GetProcessHeap(),0,3);
240 strcpy( fmtstr
, "%s" );
246 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
247 argliststart
=args
+insertnr
-1;
249 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
251 /* FIXME: precision and width components are not handled correctly */
252 if ( (strcmp(fmtstr
, "%ls") == 0) || (strcmp(fmtstr
,"%S") == 0) ) {
253 sz
= WideCharToMultiByte( CP_ACP
, 0, *(WCHAR
**)argliststart
, -1, NULL
, 0, NULL
, NULL
);
254 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
255 WideCharToMultiByte( CP_ACP
, 0, *(WCHAR
**)argliststart
, -1, b
, sz
, NULL
, NULL
);
257 b
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
= 1000);
258 /* CMF - This makes a BIG assumption about va_list */
259 TRACE("A BIG assumption\n");
260 vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
);
262 for (x
=b
; *x
; x
++) ADD_TO_T(*x
);
264 HeapFree(GetProcessHeap(),0,b
);
266 /* NULL args - copy formatstr
269 while ((lastf
<f
)&&(*lastf
)) {
273 HeapFree(GetProcessHeap(),0,fmtstr
);
320 talloced
= strlen(target
)+1;
321 if (nSize
&& talloced
<nSize
) {
322 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
324 TRACE("-- %s\n",debugstr_a(target
));
325 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
326 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
327 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
329 lstrcpynA(lpBuffer
,target
,nSize
);
331 HeapFree(GetProcessHeap(),0,target
);
332 HeapFree(GetProcessHeap(),0,from
);
333 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
334 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
335 strlen(*(LPSTR
*)lpBuffer
):
339 #endif /* __i386__ */
344 /***********************************************************************
345 * FormatMessageW (KERNEL32.@)
347 DWORD WINAPI
FormatMessageW(
356 LPDWORD args
=(LPDWORD
)_args
;
357 #if defined(__i386__) || defined(__sparc__)
358 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
362 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
366 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
367 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
368 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
369 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
370 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
372 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
373 FIXME("line wrapping not supported.\n");
375 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
376 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR
)lpSource
) + 1) *
378 strcpyW( from
, (LPCWSTR
)lpSource
);
382 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
383 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
384 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
385 from
= load_messageW( kernel32_handle
, dwMessageId
, dwLanguageId
);
389 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
393 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
397 #define ADD_TO_T(c) do {\
399 if (t-target == talloced) {\
400 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
401 t = target+talloced;\
408 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
416 WCHAR
*fmtstr
,*sprintfbuf
,*x
;
427 case '1':case '2':case '3':case '4':case '5':
428 case '6':case '7':case '8':case '9':
431 case '0':case '1':case '2':case '3':
432 case '4':case '5':case '6':case '7':
435 insertnr
=insertnr
*10+*f
-'0';
444 if (NULL
!=(x
=strchrW(f
,'!'))) {
446 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
447 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
450 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
451 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
452 f
+=strlenW(f
); /*at \0*/
456 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
457 strcpyW( fmtstr
, FMTWSTR
);
459 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
460 argliststart
=args
+insertnr
-1;
462 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
464 if (fmtstr
[strlenW(fmtstr
)-1]=='s' && argliststart
[0]) {
467 xarr
[0]=*(argliststart
+0);
468 /* possible invalid pointers */
469 xarr
[1]=*(argliststart
+1);
470 xarr
[2]=*(argliststart
+2);
471 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,(strlenW((LPWSTR
)argliststart
[0])*2+1)*sizeof(WCHAR
));
473 /* CMF - This makes a BIG assumption about va_list */
474 vsprintfW(sprintfbuf
, fmtstr
, (va_list) xarr
);
476 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,100);
478 /* CMF - This makes a BIG assumption about va_list */
479 vsprintfW(sprintfbuf
, fmtstr
, (va_list) argliststart
);
485 HeapFree(GetProcessHeap(),0,sprintfbuf
);
486 HeapFree(GetProcessHeap(),0,fmtstr
);
533 talloced
= strlenW(target
)+1;
534 if (nSize
&& talloced
<nSize
)
535 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
536 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
537 /* nSize is the MINIMUM size */
538 DWORD len
= strlenW(target
) + 1;
539 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
540 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
542 else lstrcpynW(lpBuffer
, target
, nSize
);
544 HeapFree(GetProcessHeap(),0,target
);
545 HeapFree(GetProcessHeap(),0,from
);
546 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
547 *(LPWSTR
*)lpBuffer
: lpBuffer
));
548 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
549 strlenW(*(LPWSTR
*)lpBuffer
):
553 #endif /* __i386__ */