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.@)
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)
170 from = HeapAlloc( GetProcessHeap(), 0, strlen((LPSTR)lpSource)+1 );
171 strcpy( from, (LPSTR)lpSource );
174 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
175 hmodule = GetModuleHandleA("kernel32");
176 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
179 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
182 bufsize=load_messageA(hmodule,dwMessageId,
183 MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),NULL,100);
184 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
185 MAKELANGID(LANG_NEUTRAL,SUBLANG_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_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
190 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
191 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NULL,100);
193 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
197 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
198 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
200 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
204 #define ADD_TO_T(c) do { \
206 if (t-target == talloced) {\
207 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
208 t = target+talloced;\
215 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
223 char *fmtstr,*x,*lastf;
234 case '1':case '2':case '3':case '4':case '5':
235 case '6':case '7':case '8':case '9':
238 case '0':case '1':case '2':case '3':
239 case '4':case '5':case '6':case '7':
242 insertnr=insertnr*10+*f-'0';
251 if (NULL!=(x=strchr(f,'!'))) {
253 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
254 sprintf(fmtstr,"%%%s",f);
257 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
258 sprintf(fmtstr,"%%%s",f);
259 f+=strlen(f); /*at \0*/
263 fmtstr = HeapAlloc(GetProcessHeap(),0,3);
264 strcpy( fmtstr, "%s" );
270 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
271 argliststart=args+insertnr-1;
273 argliststart=(*(DWORD**)args)+insertnr-1;
275 /* FIXME: precision and width components are not handled correctly */
276 if (strcmp(fmtstr, "%ls") == 0) {
277 sz = WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, NULL, 0, NULL, NULL);
278 b = HeapAlloc(GetProcessHeap(), 0, sz);
279 WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, b, sz, NULL, NULL);
281 b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
282 /* CMF - This makes a BIG assumption about va_list */
283 TRACE("A BIG assumption\n");
284 while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
285 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
288 for (x=b; *x; x++) ADD_TO_T(*x);
290 HeapFree(GetProcessHeap(),0,b);
292 /* NULL args - copy formatstr
295 while ((lastf<f)&&(*lastf)) {
299 HeapFree(GetProcessHeap(),0,fmtstr);
335 talloced = strlen(target)+1;
336 if (nSize && talloced<nSize) {
337 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
339 TRACE("-- %s\n",debugstr_a(target));
340 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
341 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,max(nSize, talloced));
342 memcpy(*(LPSTR*)lpBuffer,target,talloced);
344 lstrcpynA(lpBuffer,target,nSize);
346 HeapFree(GetProcessHeap(),0,target);
347 if (from) HeapFree(GetProcessHeap(),0,from);
348 TRACE("-- returning %d\n", (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer):strlen(lpBuffer));
349 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
350 strlen(*(LPSTR*)lpBuffer):
354 #endif /* __i386__ */
359 /***********************************************************************
360 * FormatMessageW (KERNEL32.@)
362 DWORD WINAPI FormatMessageW(
371 LPDWORD args=(LPDWORD)_args;
373 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
377 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
380 HMODULE hmodule = (HMODULE)lpSource;
383 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
384 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
385 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
386 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
387 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
388 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
389 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
391 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
392 FIXME("line wrapping not supported.\n");
394 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
395 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
398 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
399 hmodule = GetModuleHandleA("kernel32");
400 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
403 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
406 bufsize=load_messageA(hmodule,dwMessageId,
407 MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),NULL,100);
408 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
409 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),NULL,100);
410 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
411 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
412 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
413 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
414 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
415 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NULL,100);
417 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
421 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
422 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
424 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
428 #define ADD_TO_T(c) do {\
430 if (t-target == talloced) {\
431 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
432 t = target+talloced;\
439 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
447 char *fmtstr,*sprintfbuf,*x;
458 case '1':case '2':case '3':case '4':case '5':
459 case '6':case '7':case '8':case '9':
462 case '0':case '1':case '2':case '3':
463 case '4':case '5':case '6':case '7':
466 insertnr=insertnr*10+*f-'0';
475 if (NULL!=(x=strchr(f,'!'))) {
477 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
478 sprintf(fmtstr,"%%%s",f);
481 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
482 sprintf(fmtstr,"%%%s",f);
483 f+=strlen(f); /*at \0*/
487 fmtstr = HeapAlloc( GetProcessHeap(),0,3);
488 strcpy( fmtstr, "%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__ */