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
32 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(resource);
40 /* Messages...used by FormatMessage32* (KERNEL32.something)
42 * They can be specified either directly or using a message ID and
43 * loading them from the resource.
45 * The resourcedata has following format:
47 * 0: DWORD nrofentries
48 * nrofentries * subentry:
51 * 8: DWORD offset from start to the stringentries
53 * (lastentry-firstentry) * stringentry:
54 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
57 * (stringentry i of a subentry refers to the ID 'firstentry+i')
59 * Yes, ANSI strings in win32 resources. Go figure.
62 /**********************************************************************
63 * load_messageA (internal)
65 static INT load_messageA( HMODULE instance, UINT id, WORD lang,
66 LPSTR buffer, INT buflen )
68 const MESSAGE_RESOURCE_ENTRY *mre;
71 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
73 if (RtlFindMessage( instance, (ULONG)RT_MESSAGETABLEW, lang, id, &mre ) != STATUS_SUCCESS) return 0;
76 TRACE(" - strlen=%d\n",slen);
77 i = min(buflen - 1, slen);
81 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
82 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)mre->Text, -1, buffer, i, NULL, NULL );
84 lstrcpynA(buffer, (LPSTR)mre->Text, i);
93 TRACE("'%s' copied !\n", buffer);
98 /**********************************************************************
99 * load_messageW (internal)
101 static INT load_messageW( HMODULE instance, UINT id, WORD lang,
102 LPWSTR buffer, INT buflen )
105 LPSTR buffer2 = NULL;
106 if (buffer && buflen)
107 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
108 retval = load_messageA(instance,id,lang,buffer2,buflen);
112 lstrcpynAtoW( buffer, buffer2, buflen );
113 retval = strlenW( buffer );
115 HeapFree( GetProcessHeap(), 0, buffer2 );
122 /***********************************************************************
123 * FormatMessageA (KERNEL32.@)
124 * FIXME: missing wrap,
126 DWORD WINAPI FormatMessageA(
135 LPDWORD args=(LPDWORD)_args;
136 #if defined(__i386__) || defined(__sparc__)
137 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
141 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
144 HMODULE hmodule = (HMODULE)lpSource;
147 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
148 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
149 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
150 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
151 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
153 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
154 FIXME("line wrapping (%lu) not supported.\n", width);
156 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
158 from = HeapAlloc( GetProcessHeap(), 0, strlen((LPSTR)lpSource)+1 );
159 strcpy( from, (LPSTR)lpSource );
164 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
166 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
168 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) && (!bufsize))
170 hmodule = GetModuleHandleA("kernel32");
171 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
175 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
179 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
180 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
182 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
186 #define ADD_TO_T(c) do { \
188 if (t-target == talloced) {\
189 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
190 t = target+talloced;\
197 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
205 char *fmtstr,*x,*lastf;
216 case '1':case '2':case '3':case '4':case '5':
217 case '6':case '7':case '8':case '9':
220 case '0':case '1':case '2':case '3':
221 case '4':case '5':case '6':case '7':
224 insertnr=insertnr*10+*f-'0';
233 if (NULL!=(x=strchr(f,'!'))) {
235 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
236 sprintf(fmtstr,"%%%s",f);
239 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
240 sprintf(fmtstr,"%%%s",f);
241 f+=strlen(f); /*at \0*/
245 fmtstr = HeapAlloc(GetProcessHeap(),0,3);
246 strcpy( fmtstr, "%s" );
252 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
253 argliststart=args+insertnr-1;
255 argliststart=(*(DWORD**)args)+insertnr-1;
257 /* FIXME: precision and width components are not handled correctly */
258 if ( (strcmp(fmtstr, "%ls") == 0) || (strcmp(fmtstr,"%S") == 0) ) {
259 sz = WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, NULL, 0, NULL, NULL);
260 b = HeapAlloc(GetProcessHeap(), 0, sz);
261 WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, b, sz, NULL, NULL);
263 b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 1000);
264 /* CMF - This makes a BIG assumption about va_list */
265 TRACE("A BIG assumption\n");
266 vsnprintf(b, sz, fmtstr, (va_list) argliststart);
268 for (x=b; *x; x++) ADD_TO_T(*x);
270 HeapFree(GetProcessHeap(),0,b);
272 /* NULL args - copy formatstr
275 while ((lastf<f)&&(*lastf)) {
279 HeapFree(GetProcessHeap(),0,fmtstr);
326 talloced = strlen(target)+1;
327 if (nSize && talloced<nSize) {
328 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
330 TRACE("-- %s\n",debugstr_a(target));
331 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
332 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,max(nSize, talloced));
333 memcpy(*(LPSTR*)lpBuffer,target,talloced);
335 lstrcpynA(lpBuffer,target,nSize);
337 HeapFree(GetProcessHeap(),0,target);
338 if (from) HeapFree(GetProcessHeap(),0,from);
339 TRACE("-- returning %d\n", (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer):strlen(lpBuffer));
340 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
341 strlen(*(LPSTR*)lpBuffer):
345 #endif /* __i386__ */
350 /***********************************************************************
351 * FormatMessageW (KERNEL32.@)
353 DWORD WINAPI FormatMessageW(
362 LPDWORD args=(LPDWORD)_args;
363 #if defined(__i386__) || defined(__sparc__)
364 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
368 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
371 HMODULE hmodule = (HMODULE)lpSource;
374 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
375 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
376 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
377 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
378 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
380 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
381 FIXME("line wrapping not supported.\n");
383 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
384 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
389 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
391 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
393 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) && (!bufsize))
395 hmodule = GetModuleHandleA("kernel32");
396 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
400 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
404 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
405 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
407 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
411 #define ADD_TO_T(c) do {\
413 if (t-target == talloced) {\
414 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
415 t = target+talloced;\
422 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
430 char *fmtstr,*sprintfbuf,*x;
441 case '1':case '2':case '3':case '4':case '5':
442 case '6':case '7':case '8':case '9':
445 case '0':case '1':case '2':case '3':
446 case '4':case '5':case '6':case '7':
449 insertnr=insertnr*10+*f-'0';
458 if (NULL!=(x=strchr(f,'!'))) {
460 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
461 sprintf(fmtstr,"%%%s",f);
464 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
465 sprintf(fmtstr,"%%%s",f);
466 f+=strlen(f); /*at \0*/
470 fmtstr = HeapAlloc( GetProcessHeap(),0,3);
471 strcpy( fmtstr, "%s" );
473 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
474 argliststart=args+insertnr-1;
476 argliststart=(*(DWORD**)args)+insertnr-1;
478 if (fmtstr[strlen(fmtstr)-1]=='s' && argliststart[0]) {
481 xarr[0]=(DWORD)HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)(*(argliststart+0)));
482 /* possible invalid pointers */
483 xarr[1]=*(argliststart+1);
484 xarr[2]=*(argliststart+2);
485 sprintfbuf=HeapAlloc(GetProcessHeap(),0,strlenW((LPWSTR)argliststart[0])*2+1);
487 /* CMF - This makes a BIG assumption about va_list */
488 vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
489 HeapFree(GetProcessHeap(), 0, (LPVOID) xarr[0]);
491 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
493 /* CMF - This makes a BIG assumption about va_list */
494 vsprintf(sprintfbuf, fmtstr, (va_list) argliststart);
500 HeapFree(GetProcessHeap(),0,sprintfbuf);
501 HeapFree(GetProcessHeap(),0,fmtstr);
548 talloced = strlen(target)+1;
549 if (nSize && talloced<nSize)
550 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
551 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
552 /* nSize is the MINIMUM size */
553 DWORD len = MultiByteToWideChar( CP_ACP, 0, target, -1, NULL, 0 );
554 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,len*sizeof(WCHAR));
555 MultiByteToWideChar( CP_ACP, 0, target, -1, *(LPWSTR*)lpBuffer, len );
559 if (nSize > 0 && !MultiByteToWideChar( CP_ACP, 0, target, -1, lpBuffer, nSize ))
560 lpBuffer[nSize-1] = 0;
562 HeapFree(GetProcessHeap(),0,target);
563 if (from) HeapFree(GetProcessHeap(),0,from);
564 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
565 strlenW(*(LPWSTR*)lpBuffer):
569 #endif /* __i386__ */