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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #define WIN32_NO_STATUS
35 #include "wine/unicode.h"
36 #include "kernel_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(resource);
42 /* Messages...used by FormatMessage (KERNEL32.something)
44 * They can be specified either directly or using a message ID and
45 * loading them from the resource.
47 * The resourcedata has following format:
49 * 0: DWORD nrofentries
50 * nrofentries * subentry:
53 * 8: DWORD offset from start to the stringentries
55 * (lastentry-firstentry) * stringentry:
56 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
59 * (stringentry i of a subentry refers to the ID 'firstentry+i')
61 * Yes, ANSI strings in win32 resources. Go figure.
64 static const WCHAR PCNTFMTWSTR[] = { '%','%','%','s',0 };
65 static const WCHAR FMTWSTR[] = { '%','s',0 };
67 /**********************************************************************
68 * load_messageW (internal)
70 static LPWSTR load_messageW( HMODULE module, UINT id, WORD lang )
72 const MESSAGE_RESOURCE_ENTRY *mre;
75 TRACE("module = %p, id = %08x\n", module, id );
77 if (!module) module = GetModuleHandleW( NULL );
78 if (RtlFindMessage( module, RT_MESSAGETABLE, lang, id, &mre ) != STATUS_SUCCESS) return NULL;
80 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
82 int len = (strlenW( (const WCHAR *)mre->Text ) + 1) * sizeof(WCHAR);
83 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
84 memcpy( buffer, mre->Text, len );
88 int len = MultiByteToWideChar( CP_ACP, 0, (const char *)mre->Text, -1, NULL, 0 );
89 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
90 MultiByteToWideChar( CP_ACP, 0, (const char*)mre->Text, -1, buffer, len );
92 TRACE("returning %s\n", wine_dbgstr_w(buffer));
97 /**********************************************************************
98 * load_messageA (internal)
100 static LPSTR load_messageA( HMODULE module, UINT id, WORD lang )
102 const MESSAGE_RESOURCE_ENTRY *mre;
105 TRACE("module = %p, id = %08x\n", module, id );
107 if (!module) module = GetModuleHandleW( NULL );
108 if (RtlFindMessage( module, RT_MESSAGETABLE, lang, id, &mre ) != STATUS_SUCCESS) return NULL;
110 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
112 int len = WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, NULL, 0, NULL, NULL );
113 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
114 WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, buffer, len, NULL, NULL );
118 int len = strlen((const char*)mre->Text) + 1;
119 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
120 memcpy( buffer, mre->Text, len );
122 TRACE("returning %s\n", wine_dbgstr_a(buffer));
127 /***********************************************************************
128 * FormatMessageA (KERNEL32.@)
129 * FIXME: missing wrap,
131 DWORD WINAPI FormatMessageA(
140 LPDWORD args=(LPDWORD)_args;
142 #if defined(__i386__) || defined(__sparc__)
143 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
147 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
151 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
152 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
153 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
154 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
155 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
157 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
158 FIXME("line wrapping (%u) not supported.\n", width);
160 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
162 from = HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR)lpSource)+1 );
163 strcpy( from, (LPCSTR)lpSource );
167 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
168 from = load_messageA( (HMODULE)lpSource, dwMessageId, dwLanguageId );
169 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
170 from = load_messageA( kernel32_handle, dwMessageId, dwLanguageId );
174 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
178 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
182 #define ADD_TO_T(c) do { \
184 if ((DWORD)(t-target) == talloced) {\
185 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
186 t = target+talloced;\
193 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
201 char *fmtstr,*x,*lastf;
212 case '1':case '2':case '3':case '4':case '5':
213 case '6':case '7':case '8':case '9':
216 case '0':case '1':case '2':case '3':
217 case '4':case '5':case '6':case '7':
220 insertnr=insertnr*10+*f-'0';
229 if (NULL!=(x=strchr(f,'!'))) {
231 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
232 sprintf(fmtstr,"%%%s",f);
235 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
236 sprintf(fmtstr,"%%%s",f);
237 f+=strlen(f); /*at \0*/
241 fmtstr = HeapAlloc(GetProcessHeap(),0,3);
242 strcpy( fmtstr, "%s" );
248 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
249 argliststart=args+insertnr-1;
251 argliststart=(*(DWORD**)args)+insertnr-1;
253 /* FIXME: precision and width components are not handled correctly */
254 if ( (strcmp(fmtstr, "%ls") == 0) || (strcmp(fmtstr,"%S") == 0) ) {
255 sz = WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, NULL, 0, NULL, NULL);
256 b = HeapAlloc(GetProcessHeap(), 0, sz);
257 WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, b, sz, NULL, NULL);
259 b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 1000);
260 /* CMF - This makes a BIG assumption about va_list */
261 TRACE("A BIG assumption\n");
262 vsnprintf(b, sz, fmtstr, (va_list) argliststart);
264 for (x=b; *x; x++) ADD_TO_T(*x);
266 HeapFree(GetProcessHeap(),0,b);
268 /* NULL args - copy formatstr
271 while ((lastf<f)&&(*lastf)) {
275 HeapFree(GetProcessHeap(),0,fmtstr);
322 talloced = strlen(target)+1;
323 if (nSize && talloced<nSize) {
324 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
326 TRACE("-- %s\n",debugstr_a(target));
327 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
328 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(LMEM_ZEROINIT,max(nSize, talloced));
329 memcpy(*(LPSTR*)lpBuffer,target,talloced);
331 lstrcpynA(lpBuffer,target,nSize);
333 HeapFree(GetProcessHeap(),0,target);
334 HeapFree(GetProcessHeap(),0,from);
335 ret = (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer) : strlen(lpBuffer);
336 #endif /* __i386__ */
337 TRACE("-- returning %d\n", ret);
343 /***********************************************************************
344 * FormatMessageW (KERNEL32.@)
346 DWORD WINAPI FormatMessageW(
355 LPDWORD args=(LPDWORD)_args;
356 #if defined(__i386__) || defined(__sparc__)
357 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
361 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
365 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
366 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
367 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
368 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
369 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
371 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
372 FIXME("line wrapping not supported.\n");
374 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
375 from = HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR)lpSource) + 1) *
377 strcpyW( from, (LPCWSTR)lpSource );
381 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
382 from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
383 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
384 from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
388 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
392 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) );
396 #define ADD_TO_T(c) do {\
398 if ((DWORD)(t-target) == talloced) {\
399 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
400 t = target+talloced;\
407 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
415 WCHAR *fmtstr,*sprintfbuf,*x;
426 case '1':case '2':case '3':case '4':case '5':
427 case '6':case '7':case '8':case '9':
430 case '0':case '1':case '2':case '3':
431 case '4':case '5':case '6':case '7':
434 insertnr=insertnr*10+*f-'0';
443 if (NULL!=(x=strchrW(f,'!'))) {
445 fmtstr=HeapAlloc( GetProcessHeap(), 0,(strlenW(f)+2)*sizeof(WCHAR));
446 sprintfW(fmtstr,PCNTFMTWSTR,f);
449 fmtstr=HeapAlloc(GetProcessHeap(),0,(strlenW(f)+2)*sizeof(WCHAR));
450 sprintfW(fmtstr,PCNTFMTWSTR,f);
451 f+=strlenW(f); /*at \0*/
455 fmtstr = HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR));
456 strcpyW( fmtstr, FMTWSTR );
458 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
459 argliststart=args+insertnr-1;
461 argliststart=(*(DWORD**)args)+insertnr-1;
463 if (fmtstr[strlenW(fmtstr)-1]=='s' && argliststart[0]) {
466 xarr[0]=*(argliststart+0);
467 /* possible invalid pointers */
468 xarr[1]=*(argliststart+1);
469 xarr[2]=*(argliststart+2);
470 sprintfbuf=HeapAlloc(GetProcessHeap(),0,(strlenW((LPWSTR)argliststart[0])*2+1)*sizeof(WCHAR));
472 /* CMF - This makes a BIG assumption about va_list */
473 vsprintfW(sprintfbuf, fmtstr, (va_list) xarr);
475 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
477 /* CMF - This makes a BIG assumption about va_list */
478 vsprintfW(sprintfbuf, fmtstr, (va_list) argliststart);
484 HeapFree(GetProcessHeap(),0,sprintfbuf);
485 HeapFree(GetProcessHeap(),0,fmtstr);
532 talloced = strlenW(target)+1;
533 if (nSize && talloced<nSize)
534 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize*sizeof(WCHAR));
535 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
536 /* nSize is the MINIMUM size */
537 DWORD len = strlenW(target) + 1;
538 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(LMEM_ZEROINIT,len*sizeof(WCHAR));
539 strcpyW(*(LPWSTR*)lpBuffer, target);
541 else lstrcpynW(lpBuffer, target, nSize);
543 HeapFree(GetProcessHeap(),0,target);
544 HeapFree(GetProcessHeap(),0,from);
545 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
546 *(LPWSTR*)lpBuffer : lpBuffer));
547 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
548 strlenW(*(LPWSTR*)lpBuffer):
552 #endif /* __i386__ */