Fixed format strings.
[wine/wine64.git] / dlls / kernel / format_msg.c
blobeab792227fc078662f26cc75a24143efba6602cb
1 /*
2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
5 */
7 #include "config.h"
9 #include <stdio.h>
10 #include <string.h>
12 #include "windef.h"
13 #include "winbase.h"
14 #include "winerror.h"
15 #include "winnls.h"
16 #include "wine/unicode.h"
18 #include "heap.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:
30 * start:
31 * 0: DWORD nrofentries
32 * nrofentries * subentry:
33 * 0: DWORD firstentry
34 * 4: DWORD lastentry
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 ]
39 * 2: WORD flags
40 * 4: CHAR[len-4]
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 )
52 HGLOBAL hmem;
53 HRSRC hrsrc;
54 PMESSAGE_RESOURCE_DATA mrd;
55 PMESSAGE_RESOURCE_BLOCK mrb;
56 PMESSAGE_RESOURCE_ENTRY mre;
57 int i,slen;
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);
63 if (!hrsrc) return 0;
64 hmem = LoadResource( instance, hrsrc );
65 if (!hmem) return 0;
67 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
68 mre = NULL;
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);
73 id -= mrb->LowId;
74 break;
76 mrb++;
78 if (!mre)
79 return 0;
80 for (i=id;i--;) {
81 if (!mre->Length)
82 return 0;
83 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+mre->Length);
85 slen=mre->Length;
86 TRACE(" - strlen=%d\n",slen);
87 i = min(buflen - 1, slen);
88 if (buffer == NULL)
89 return slen;
90 if (i>0) {
91 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
92 lstrcpynWtoA(buffer, (LPWSTR)mre->Text, i);
93 else
94 lstrcpynA(buffer, (LPSTR)mre->Text, i);
95 buffer[i]=0;
96 } else {
97 if (buflen>1) {
98 buffer[0]=0;
99 return 0;
102 if (buffer)
103 TRACE("'%s' copied !\n", buffer);
104 return i;
107 #if 0 /* FIXME */
108 /**********************************************************************
109 * load_messageW (internal)
111 static INT load_messageW( HMODULE instance, UINT id, WORD lang,
112 LPWSTR buffer, INT buflen )
114 INT retval;
115 LPSTR buffer2 = NULL;
116 if (buffer && buflen)
117 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
118 retval = load_messageA(instance,id,lang,buffer2,buflen);
119 if (buffer)
121 if (retval) {
122 lstrcpynAtoW( buffer, buffer2, buflen );
123 retval = strlenW( buffer );
125 HeapFree( GetProcessHeap(), 0, buffer2 );
127 return retval;
129 #endif
132 /***********************************************************************
133 * FormatMessageA (KERNEL32.138)
134 * FIXME: missing wrap,
136 DWORD WINAPI FormatMessageA(
137 DWORD dwFlags,
138 LPCVOID lpSource,
139 DWORD dwMessageId,
140 DWORD dwLanguageId,
141 LPSTR lpBuffer,
142 DWORD nSize,
143 LPDWORD args /* va_list *args */
145 #ifdef __i386__
146 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
147 LPSTR target,t;
148 DWORD talloced;
149 LPSTR from,f;
150 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
151 BOOL eos = FALSE;
152 INT bufsize;
153 HMODULE hmodule = (HMODULE)lpSource;
154 CHAR ch;
156 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
157 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
158 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
159 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
160 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
161 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
162 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
164 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
165 FIXME("line wrapping (%lu) not supported.\n", width);
166 from = NULL;
167 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
168 from = HEAP_strdupA( GetProcessHeap(), 0, (LPSTR)lpSource);
170 else {
171 dwMessageId &= 0xFFFF;
172 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
173 hmodule = GetModuleHandleA("kernel32");
174 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
175 if (!bufsize) {
176 if (dwLanguageId) {
177 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
178 return 0;
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);
190 if (!bufsize) {
191 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
192 return 0;
195 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
196 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
198 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
199 t = target;
200 talloced= 100;
202 #define ADD_TO_T(c) do { \
203 *t++=c;\
204 if (t-target == talloced) {\
205 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
206 t = target+talloced;\
207 talloced*=2;\
209 } while (0)
211 if (from) {
212 f=from;
213 while (*f && !eos) {
214 if (*f=='%') {
215 int insertnr;
216 char *fmtstr,*x,*lastf;
217 DWORD *argliststart;
219 fmtstr = NULL;
220 lastf = f;
221 f++;
222 if (!*f) {
223 ADD_TO_T('%');
224 continue;
226 switch (*f) {
227 case '1':case '2':case '3':case '4':case '5':
228 case '6':case '7':case '8':case '9':
229 insertnr=*f-'0';
230 switch (f[1]) {
231 case '0':case '1':case '2':case '3':
232 case '4':case '5':case '6':case '7':
233 case '8':case '9':
234 f++;
235 insertnr=insertnr*10+*f-'0';
236 f++;
237 break;
238 default:
239 f++;
240 break;
242 if (*f=='!') {
243 f++;
244 if (NULL!=(x=strchr(f,'!'))) {
245 *x='\0';
246 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
247 sprintf(fmtstr,"%%%s",f);
248 f=x+1;
249 } else {
250 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
251 sprintf(fmtstr,"%%%s",f);
252 f+=strlen(f); /*at \0*/
254 } else
255 if(!args)
256 break;
257 else
258 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
259 if (args) {
260 int sz;
261 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
263 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
264 argliststart=args+insertnr-1;
265 else
266 argliststart=(*(DWORD**)args)+insertnr-1;
268 /* CMF - This makes a BIG assumption about va_list */
269 while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
270 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
272 for (x=b; *x; x++) ADD_TO_T(*x);
274 HeapFree(GetProcessHeap(),0,b);
275 } else {
276 /* NULL args - copy formatstr
277 * (probably wrong)
279 while ((lastf<f)&&(*lastf)) {
280 ADD_TO_T(*lastf++);
283 HeapFree(GetProcessHeap(),0,fmtstr);
284 break;
285 case 'n':
286 ADD_TO_T('\r');
287 ADD_TO_T('\n');
288 f++;
289 break;
290 case '0':
291 eos = TRUE;
292 f++;
293 break;
294 default:
295 ADD_TO_T(*f++);
296 break;
298 } else {
299 ch = *f;
300 f++;
301 if (ch == '\r')
303 if (*f == '\n')
304 f++;
305 ADD_TO_T(' ');
307 else
308 if (ch == '\n')
310 ADD_TO_T('\r');
311 ADD_TO_T('\n');
313 else
314 ADD_TO_T(ch);
317 *t='\0';
319 talloced = strlen(target)+1;
320 if (nSize && talloced<nSize) {
321 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
323 TRACE("-- %s\n",debugstr_a(target));
324 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
325 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,max(nSize, talloced));
326 memcpy(*(LPSTR*)lpBuffer,target,talloced);
327 } else {
328 lstrcpynA(lpBuffer,target,nSize);
330 HeapFree(GetProcessHeap(),0,target);
331 if (from) HeapFree(GetProcessHeap(),0,from);
332 TRACE("-- returning %d\n", (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer):strlen(lpBuffer));
333 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
334 strlen(*(LPSTR*)lpBuffer):
335 strlen(lpBuffer);
336 #else
337 return 0;
338 #endif /* __i386__ */
340 #undef ADD_TO_T
343 /***********************************************************************
344 * FormatMessageW (KERNEL32.138)
346 DWORD WINAPI FormatMessageW(
347 DWORD dwFlags,
348 LPCVOID lpSource,
349 DWORD dwMessageId,
350 DWORD dwLanguageId,
351 LPWSTR lpBuffer,
352 DWORD nSize,
353 LPDWORD args /* va_list *args */
355 #ifdef __i386__
356 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
357 LPSTR target,t;
358 DWORD talloced;
359 LPSTR from,f;
360 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
361 BOOL eos = FALSE;
362 INT bufsize;
363 HMODULE hmodule = (HMODULE)lpSource;
364 CHAR ch;
366 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
367 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
368 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
369 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
370 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
371 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
372 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
374 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
375 FIXME("line wrapping not supported.\n");
376 from = NULL;
377 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
378 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
380 else {
381 dwMessageId &= 0xFFFF;
382 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
383 hmodule = GetModuleHandleA("kernel32");
384 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
385 if (!bufsize) {
386 if (dwLanguageId) {
387 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
388 return 0;
390 bufsize=load_messageA(hmodule,dwMessageId,
391 MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),NULL,100);
392 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
393 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),NULL,100);
394 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
395 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
396 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
397 MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),NULL,100);
398 if (!bufsize) bufsize=load_messageA(hmodule,dwMessageId,
399 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NULL,100);
400 if (!bufsize) {
401 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
402 return 0;
405 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
406 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
408 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
409 t = target;
410 talloced= 100;
412 #define ADD_TO_T(c) do {\
413 *t++=c;\
414 if (t-target == talloced) {\
415 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
416 t = target+talloced;\
417 talloced*=2;\
419 } while (0)
421 if (from) {
422 f=from;
423 while (*f && !eos) {
424 if (*f=='%') {
425 int insertnr;
426 char *fmtstr,*sprintfbuf,*x;
427 DWORD *argliststart;
429 fmtstr = NULL;
430 f++;
431 if (!*f) {
432 ADD_TO_T('%');
433 continue;
435 switch (*f) {
436 case '1':case '2':case '3':case '4':case '5':
437 case '6':case '7':case '8':case '9':
438 insertnr=*f-'0';
439 switch (f[1]) {
440 case '0':case '1':case '2':case '3':
441 case '4':case '5':case '6':case '7':
442 case '8':case '9':
443 f++;
444 insertnr=insertnr*10+*f-'0';
445 f++;
446 break;
447 default:
448 f++;
449 break;
451 if (*f=='!') {
452 f++;
453 if (NULL!=(x=strchr(f,'!')))
455 *x='\0';
456 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
457 sprintf(fmtstr,"%%%s",f);
458 f=x+1;
459 } else {
460 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
461 sprintf(fmtstr,"%%%s",f);
462 f+=strlen(f); /*at \0*/
464 } else
465 if(!args)
466 break;
467 else
468 fmtstr=HEAP_strdupA( GetProcessHeap(),0,"%s");
469 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
470 argliststart=args+insertnr-1;
471 else
472 argliststart=(*(DWORD**)args)+insertnr-1;
474 if (fmtstr[strlen(fmtstr)-1]=='s' && argliststart[0]) {
475 DWORD xarr[3];
477 xarr[0]=(DWORD)HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)(*(argliststart+0)));
478 /* possible invalid pointers */
479 xarr[1]=*(argliststart+1);
480 xarr[2]=*(argliststart+2);
481 sprintfbuf=HeapAlloc(GetProcessHeap(),0,strlenW((LPWSTR)argliststart[0])*2+1);
483 /* CMF - This makes a BIG assumption about va_list */
484 vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
485 } else {
486 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
488 /* CMF - This makes a BIG assumption about va_list */
489 vsprintf(sprintfbuf, fmtstr, (va_list) argliststart);
491 x=sprintfbuf;
492 while (*x) {
493 ADD_TO_T(*x++);
495 HeapFree(GetProcessHeap(),0,sprintfbuf);
496 HeapFree(GetProcessHeap(),0,fmtstr);
497 break;
498 case 'n':
499 ADD_TO_T('\r');
500 ADD_TO_T('\n');
501 f++;
502 break;
503 case '0':
504 eos = TRUE;
505 f++;
506 break;
507 default:
508 ADD_TO_T(*f++);
509 break;
511 } else {
512 ch = *f;
513 f++;
514 if (ch == '\r')
516 if (*f == '\n')
517 f++;
518 ADD_TO_T(' ');
520 else
521 if (ch == '\n')
523 ADD_TO_T('\r');
524 ADD_TO_T('\n');
526 else
527 ADD_TO_T(ch);
530 *t='\0';
532 talloced = strlen(target)+1;
533 if (nSize && talloced<nSize)
534 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
535 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
536 /* nSize is the MINIMUM size */
537 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,talloced*2+2);
538 lstrcpynAtoW(*(LPWSTR*)lpBuffer,target,talloced);
539 } else
540 lstrcpynAtoW(lpBuffer,target,nSize);
541 HeapFree(GetProcessHeap(),0,target);
542 if (from) HeapFree(GetProcessHeap(),0,from);
543 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
544 strlenW(*(LPWSTR*)lpBuffer):
545 strlenW(lpBuffer);
546 #else
547 return 0;
548 #endif /* __i386__ */
550 #undef ADD_TO_T