2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
16 #include "wine/unicode.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(resource);
24 /* Messages...used by FormatMessage32* (KERNEL32.something)
26 * They can be specified either directly or using a message ID and
27 * loading them from the resource.
29 * The resourcedata has following format:
31 * 0: DWORD nrofentries
32 * nrofentries * subentry:
35 * 8: DWORD offset from start to the stringentries
37 * (lastentry-firstentry) * stringentry:
38 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
41 * (stringentry i of a subentry refers to the ID 'firstentry+i')
43 * Yes, ANSI strings in win32 resources. Go figure.
46 /**********************************************************************
47 * load_messageA (internal)
49 static INT load_messageA( HMODULE instance, UINT id, WORD lang,
50 LPSTR buffer, INT buflen )
54 PMESSAGE_RESOURCE_DATA mrd;
55 PMESSAGE_RESOURCE_BLOCK mrb;
56 PMESSAGE_RESOURCE_ENTRY mre;
59 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
61 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
62 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
64 hmem = LoadResource( instance, hrsrc );
67 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
69 mrb = &(mrd->Blocks[0]);
70 for (i=mrd->NumberOfBlocks;i--;) {
71 if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
72 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
83 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+mre->Length);
86 TRACE(" - strlen=%d\n",slen);
87 i = min(buflen - 1, slen);
91 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
92 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)mre->Text, -1, buffer, i, NULL, NULL );
94 lstrcpynA(buffer, (LPSTR)mre->Text, i);
103 TRACE("'%s' copied !\n", buffer);
108 /**********************************************************************
109 * load_messageW (internal)
111 static INT load_messageW( HMODULE instance, UINT id, WORD lang,
112 LPWSTR buffer, INT buflen )
115 LPSTR buffer2 = NULL;
116 if (buffer && buflen)
117 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
118 retval = load_messageA(instance,id,lang,buffer2,buflen);
122 lstrcpynAtoW( buffer, buffer2, buflen );
123 retval = strlenW( buffer );
125 HeapFree( GetProcessHeap(), 0, buffer2 );
132 /***********************************************************************
133 * FormatMessageA (KERNEL32.265)
134 * FIXME: missing wrap,
136 DWORD WINAPI FormatMessageA(
145 LPDWORD args=(LPDWORD)_args;
147 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
151 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
154 HMODULE hmodule = (HMODULE)lpSource;
157 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
158 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
159 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
160 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
161 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
162 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
163 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
165 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
166 FIXME("line wrapping (%lu) not supported.\n", width);
168 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
169 from = HEAP_strdupA( GetProcessHeap(), 0, (LPSTR)lpSource);
172 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
173 hmodule = GetModuleHandleA("kernel32");
174 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
177 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
180 bufsize=load_messageA(hmodule,dwMessageId,
181 MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),NULL,100);
182 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
183 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),NULL,100);
184 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
185 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
186 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
187 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
188 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
189 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NULL,100);
191 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
195 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
196 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
198 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
202 #define ADD_TO_T(c) do { \
204 if (t-target == talloced) {\
205 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
206 t = target+talloced;\
213 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
221 char *fmtstr,*x,*lastf;
232 case '1':case '2':case '3':case '4':case '5':
233 case '6':case '7':case '8':case '9':
236 case '0':case '1':case '2':case '3':
237 case '4':case '5':case '6':case '7':
240 insertnr=insertnr*10+*f-'0';
249 if (NULL!=(x=strchr(f,'!'))) {
251 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
252 sprintf(fmtstr,"%%%s",f);
255 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
256 sprintf(fmtstr,"%%%s",f);
257 f+=strlen(f); /*at \0*/
263 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
269 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
270 argliststart=args+insertnr-1;
272 argliststart=(*(DWORD**)args)+insertnr-1;
274 /* FIXME: precision and width components are not handled correctly */
275 if (strcmp(fmtstr, "%ls") == 0) {
276 sz = WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, NULL, 0, NULL, NULL);
277 b = HeapAlloc(GetProcessHeap(), 0, sz);
278 WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, b, sz, NULL, NULL);
280 b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
281 /* CMF - This makes a BIG assumption about va_list */
282 TRACE("A BIG assumption\n");
283 while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
284 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
287 for (x=b; *x; x++) ADD_TO_T(*x);
289 HeapFree(GetProcessHeap(),0,b);
291 /* NULL args - copy formatstr
294 while ((lastf<f)&&(*lastf)) {
298 HeapFree(GetProcessHeap(),0,fmtstr);
334 talloced = strlen(target)+1;
335 if (nSize && talloced<nSize) {
336 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
338 TRACE("-- %s\n",debugstr_a(target));
339 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
340 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,max(nSize, talloced));
341 memcpy(*(LPSTR*)lpBuffer,target,talloced);
343 lstrcpynA(lpBuffer,target,nSize);
345 HeapFree(GetProcessHeap(),0,target);
346 if (from) HeapFree(GetProcessHeap(),0,from);
347 TRACE("-- returning %d\n", (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer):strlen(lpBuffer));
348 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
349 strlen(*(LPSTR*)lpBuffer):
353 #endif /* __i386__ */
358 /***********************************************************************
359 * FormatMessageW (KERNEL32.266)
361 DWORD WINAPI FormatMessageW(
370 LPDWORD args=(LPDWORD)_args;
372 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
376 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
379 HMODULE hmodule = (HMODULE)lpSource;
382 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
383 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
384 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
385 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
386 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
387 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
388 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
390 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
391 FIXME("line wrapping not supported.\n");
393 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
394 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
397 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
398 hmodule = GetModuleHandleA("kernel32");
399 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
402 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
405 bufsize=load_messageA(hmodule,dwMessageId,
406 MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),NULL,100);
407 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
408 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),NULL,100);
409 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
410 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
411 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
412 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
413 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
414 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NULL,100);
416 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
420 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
421 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
423 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
427 #define ADD_TO_T(c) do {\
429 if (t-target == talloced) {\
430 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
431 t = target+talloced;\
438 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
446 char *fmtstr,*sprintfbuf,*x;
457 case '1':case '2':case '3':case '4':case '5':
458 case '6':case '7':case '8':case '9':
461 case '0':case '1':case '2':case '3':
462 case '4':case '5':case '6':case '7':
465 insertnr=insertnr*10+*f-'0';
474 if (NULL!=(x=strchr(f,'!'))) {
476 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
477 sprintf(fmtstr,"%%%s",f);
480 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
481 sprintf(fmtstr,"%%%s",f);
482 f+=strlen(f); /*at \0*/
488 fmtstr=HEAP_strdupA( GetProcessHeap(),0,"%s");
490 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
491 argliststart=args+insertnr-1;
493 argliststart=(*(DWORD**)args)+insertnr-1;
495 if (fmtstr[strlen(fmtstr)-1]=='s' && argliststart[0]) {
498 xarr[0]=(DWORD)HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)(*(argliststart+0)));
499 /* possible invalid pointers */
500 xarr[1]=*(argliststart+1);
501 xarr[2]=*(argliststart+2);
502 sprintfbuf=HeapAlloc(GetProcessHeap(),0,strlenW((LPWSTR)argliststart[0])*2+1);
504 /* CMF - This makes a BIG assumption about va_list */
505 vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
507 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
509 /* CMF - This makes a BIG assumption about va_list */
510 vsprintf(sprintfbuf, fmtstr, (va_list) argliststart);
516 HeapFree(GetProcessHeap(),0,sprintfbuf);
517 HeapFree(GetProcessHeap(),0,fmtstr);
553 talloced = strlen(target)+1;
554 if (nSize && talloced<nSize)
555 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
556 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
557 /* nSize is the MINIMUM size */
558 DWORD len = MultiByteToWideChar( CP_ACP, 0, target, -1, NULL, 0 );
559 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,len*sizeof(WCHAR));
560 MultiByteToWideChar( CP_ACP, 0, target, -1, *(LPWSTR*)lpBuffer, len );
564 if (nSize > 0 && !MultiByteToWideChar( CP_ACP, 0, target, -1, lpBuffer, nSize ))
565 lpBuffer[nSize-1] = 0;
567 HeapFree(GetProcessHeap(),0,target);
568 if (from) HeapFree(GetProcessHeap(),0,from);
569 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
570 strlenW(*(LPWSTR*)lpBuffer):
574 #endif /* __i386__ */