dlls: Fix va_list -> ms_va_list for variadic api calls
[wine/wine64.git] / dlls / kernel32 / format_msg.c
blob3fcefee4ff62d986dbc485754924ad1f3435e7eb
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winternl.h"
33 #include "winuser.h"
34 #include "winnls.h"
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
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:
48 * start:
49 * 0: DWORD nrofentries
50 * nrofentries * subentry:
51 * 0: DWORD firstentry
52 * 4: DWORD lastentry
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 ]
57 * 2: WORD flags
58 * 4: CHAR[len-4]
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;
73 WCHAR *buffer;
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 );
86 else
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));
93 return buffer;
97 /**********************************************************************
98 * load_messageA (internal)
100 static LPSTR load_messageA( HMODULE module, UINT id, WORD lang )
102 const MESSAGE_RESOURCE_ENTRY *mre;
103 char *buffer;
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 );
116 else
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));
123 return buffer;
127 /***********************************************************************
128 * FormatMessageA (KERNEL32.@)
129 * FIXME: missing wrap,
131 DWORD WINAPI FormatMessageA(
132 DWORD dwFlags,
133 LPCVOID lpSource,
134 DWORD dwMessageId,
135 DWORD dwLanguageId,
136 LPSTR lpBuffer,
137 DWORD nSize,
138 ms_va_list* _args )
140 LPDWORD args=(LPDWORD)_args;
141 DWORD ret = 0;
142 #if defined(__i386__) || defined(__sparc__)
143 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
144 LPSTR target,t;
145 DWORD talloced;
146 LPSTR from,f;
147 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
148 BOOL eos = FALSE;
149 CHAR ch;
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 (!lpBuffer)
159 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
160 return 0;
163 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
164 FIXME("line wrapping (%u) not supported.\n", width);
165 from = NULL;
166 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
168 from = HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR)lpSource)+1 );
169 strcpy( from, (LPCSTR)lpSource );
171 else {
172 from = NULL;
173 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
174 from = load_messageA( (HMODULE)lpSource, dwMessageId, dwLanguageId );
175 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
176 from = load_messageA( kernel32_handle, dwMessageId, dwLanguageId );
178 if (!from)
180 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
181 return 0;
184 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
185 t = target;
186 talloced= 100;
188 #define ADD_TO_T(c) do { \
189 *t++=c;\
190 if ((DWORD)(t-target) == talloced) {\
191 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
192 t = target+talloced;\
193 talloced*=2;\
195 } while (0)
197 if (from) {
198 f=from;
199 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
200 while (*f && !eos)
201 ADD_TO_T(*f++);
203 else {
204 while (*f && !eos) {
205 if (*f=='%') {
206 int insertnr;
207 char *fmtstr,*x,*lastf;
208 DWORD *argliststart;
210 fmtstr = NULL;
211 lastf = f;
212 f++;
213 if (!*f) {
214 ADD_TO_T('%');
215 continue;
217 switch (*f) {
218 case '1':case '2':case '3':case '4':case '5':
219 case '6':case '7':case '8':case '9':
220 insertnr=*f-'0';
221 switch (f[1]) {
222 case '0':case '1':case '2':case '3':
223 case '4':case '5':case '6':case '7':
224 case '8':case '9':
225 f++;
226 insertnr=insertnr*10+*f-'0';
227 f++;
228 break;
229 default:
230 f++;
231 break;
233 if (*f=='!') {
234 f++;
235 if (NULL!=(x=strchr(f,'!'))) {
236 *x='\0';
237 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
238 sprintf(fmtstr,"%%%s",f);
239 f=x+1;
240 } else {
241 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
242 sprintf(fmtstr,"%%%s",f);
243 f+=strlen(f); /*at \0*/
245 } else {
246 if(!args) break;
247 fmtstr = HeapAlloc(GetProcessHeap(),0,3);
248 strcpy( fmtstr, "%s" );
250 if (args) {
251 int sz;
252 LPSTR b;
254 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
255 argliststart=args+insertnr-1;
256 else
257 argliststart=(*(DWORD**)args)+insertnr-1;
259 /* FIXME: precision and width components are not handled correctly */
260 if ( (strcmp(fmtstr, "%ls") == 0) || (strcmp(fmtstr,"%S") == 0) ) {
261 sz = WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, NULL, 0, NULL, NULL);
262 b = HeapAlloc(GetProcessHeap(), 0, sz);
263 WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, b, sz, NULL, NULL);
264 } else {
265 b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 1000);
266 /* CMF - This makes a BIG assumption about va_list */
267 TRACE("A BIG assumption\n");
268 #ifdef _WIN64
269 /* We need to link to msvcrt here */
270 ERR("This code is broken for WIN64\n");
271 #endif
272 vsnprintf(b, sz, fmtstr, (va_list) argliststart);
274 for (x=b; *x; x++) ADD_TO_T(*x);
276 HeapFree(GetProcessHeap(),0,b);
277 } else {
278 /* NULL args - copy formatstr
279 * (probably wrong)
281 while ((lastf<f)&&(*lastf)) {
282 ADD_TO_T(*lastf++);
285 HeapFree(GetProcessHeap(),0,fmtstr);
286 break;
287 case 'n':
288 ADD_TO_T('\r');
289 ADD_TO_T('\n');
290 f++;
291 break;
292 case '0':
293 eos = TRUE;
294 f++;
295 break;
296 default:
297 ADD_TO_T(*f++);
298 break;
300 } else {
301 ch = *f;
302 f++;
303 if (ch == '\r') {
304 if (*f == '\n')
305 f++;
306 if(width)
307 ADD_TO_T(' ');
308 else
310 ADD_TO_T('\r');
311 ADD_TO_T('\n');
313 } else {
314 if (ch == '\n')
316 if(width)
317 ADD_TO_T(' ');
318 else
320 ADD_TO_T('\r');
321 ADD_TO_T('\n');
324 else
325 ADD_TO_T(ch);
330 *t='\0';
332 talloced = strlen(target)+1;
333 if (nSize && talloced<nSize) {
334 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
336 TRACE("-- %s\n",debugstr_a(target));
337 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
338 *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT,max(nSize, talloced));
339 memcpy(*(LPSTR*)lpBuffer,target,talloced);
340 } else {
341 lstrcpynA(lpBuffer,target,nSize);
343 HeapFree(GetProcessHeap(),0,target);
344 HeapFree(GetProcessHeap(),0,from);
345 ret = (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer) : strlen(lpBuffer);
346 #endif /* __i386__ */
347 TRACE("-- returning %d\n", ret);
348 return ret;
350 #undef ADD_TO_T
353 /***********************************************************************
354 * FormatMessageW (KERNEL32.@)
356 DWORD WINAPI FormatMessageW(
357 DWORD dwFlags,
358 LPCVOID lpSource,
359 DWORD dwMessageId,
360 DWORD dwLanguageId,
361 LPWSTR lpBuffer,
362 DWORD nSize,
363 ms_va_list* _args )
365 LPDWORD args=(LPDWORD)_args;
366 #if defined(__i386__) || defined(__sparc__)
367 /* This implementation is completely dependent on the format of the ms_va_list on x86 CPUs */
368 LPWSTR target,t;
369 DWORD talloced;
370 LPWSTR from,f;
371 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
372 BOOL eos = FALSE;
373 WCHAR ch;
375 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
376 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
377 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
378 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
379 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
381 if (!lpBuffer)
383 SetLastError(ERROR_INVALID_PARAMETER);
384 return 0;
387 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
388 FIXME("line wrapping not supported.\n");
389 from = NULL;
390 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
391 from = HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR)lpSource) + 1) *
392 sizeof(WCHAR) );
393 strcpyW( from, (LPCWSTR)lpSource );
395 else {
396 from = NULL;
397 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
398 from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
399 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
400 from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
402 if (!from)
404 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
405 return 0;
408 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) );
409 t = target;
410 talloced= 100;
412 #define ADD_TO_T(c) do {\
413 *t++=c;\
414 if ((DWORD)(t-target) == talloced) {\
415 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
416 t = target+talloced;\
417 talloced*=2;\
419 } while (0)
421 if (from) {
422 f=from;
423 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
424 while (*f && !eos)
425 ADD_TO_T(*f++);
427 else {
428 while (*f && !eos) {
429 if (*f=='%') {
430 int insertnr;
431 WCHAR *fmtstr,*sprintfbuf,*x;
432 DWORD *argliststart;
434 fmtstr = NULL;
435 f++;
436 if (!*f) {
437 ADD_TO_T('%');
438 continue;
441 switch (*f) {
442 case '1':case '2':case '3':case '4':case '5':
443 case '6':case '7':case '8':case '9':
444 insertnr=*f-'0';
445 switch (f[1]) {
446 case '0':case '1':case '2':case '3':
447 case '4':case '5':case '6':case '7':
448 case '8':case '9':
449 f++;
450 insertnr=insertnr*10+*f-'0';
451 f++;
452 break;
453 default:
454 f++;
455 break;
457 if (*f=='!') {
458 f++;
459 if (NULL!=(x=strchrW(f,'!'))) {
460 *x='\0';
461 fmtstr=HeapAlloc( GetProcessHeap(), 0,(strlenW(f)+2)*sizeof(WCHAR));
462 sprintfW(fmtstr,PCNTFMTWSTR,f);
463 f=x+1;
464 } else {
465 fmtstr=HeapAlloc(GetProcessHeap(),0,(strlenW(f)+2)*sizeof(WCHAR));
466 sprintfW(fmtstr,PCNTFMTWSTR,f);
467 f+=strlenW(f); /*at \0*/
469 } else {
470 if(!args) break;
471 fmtstr = HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR));
472 strcpyW( fmtstr, FMTWSTR );
474 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
475 argliststart=args+insertnr-1;
476 else
477 argliststart=(*(DWORD**)args)+insertnr-1;
479 if (fmtstr[strlenW(fmtstr)-1]=='s' && argliststart[0]) {
480 DWORD xarr[3];
482 xarr[0]=*(argliststart+0);
483 /* possible invalid pointers */
484 xarr[1]=*(argliststart+1);
485 xarr[2]=*(argliststart+2);
486 sprintfbuf=HeapAlloc(GetProcessHeap(),0,(strlenW((LPWSTR)argliststart[0])*2+1)*sizeof(WCHAR));
488 /* CMF - This makes a BIG assumption about ms_va_list */
489 vsprintfW(sprintfbuf, fmtstr, (ms_va_list) xarr);
490 } else {
491 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
493 /* CMF - This makes a BIG assumption about ms_va_list */
494 vsprintfW(sprintfbuf, fmtstr, (ms_va_list) argliststart);
496 x=sprintfbuf;
497 while (*x) {
498 ADD_TO_T(*x++);
500 HeapFree(GetProcessHeap(),0,sprintfbuf);
501 HeapFree(GetProcessHeap(),0,fmtstr);
502 break;
503 case 'n':
504 ADD_TO_T('\r');
505 ADD_TO_T('\n');
506 f++;
507 break;
508 case '0':
509 eos = TRUE;
510 f++;
511 break;
512 default:
513 ADD_TO_T(*f++);
514 break;
516 } else {
517 ch = *f;
518 f++;
519 if (ch == '\r') {
520 if (*f == '\n')
521 f++;
522 if(width)
523 ADD_TO_T(' ');
524 else
526 ADD_TO_T('\r');
527 ADD_TO_T('\n');
529 } else {
530 if (ch == '\n')
532 if(width)
533 ADD_TO_T(' ');
534 else
536 ADD_TO_T('\r');
537 ADD_TO_T('\n');
540 else
541 ADD_TO_T(ch);
546 *t='\0';
548 talloced = strlenW(target)+1;
549 if (nSize && talloced<nSize)
550 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize*sizeof(WCHAR));
551 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
552 /* nSize is the MINIMUM size */
553 DWORD len = strlenW(target) + 1;
554 *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT,len*sizeof(WCHAR));
555 strcpyW(*(LPWSTR*)lpBuffer, target);
557 else lstrcpynW(lpBuffer, target, nSize);
559 HeapFree(GetProcessHeap(),0,target);
560 HeapFree(GetProcessHeap(),0,from);
561 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
562 *(LPWSTR*)lpBuffer : lpBuffer));
563 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
564 strlenW(*(LPWSTR*)lpBuffer):
565 strlenW(lpBuffer);
566 #else
567 ERR("Not implemented!\n");
568 return 0;
569 #endif /* __i386__ */
571 #undef ADD_TO_T