For all DLLs with defined DllMain and which do not require
[wine] / dlls / kernel / format_msg.c
1 /*
2  * FormatMessage implementation
3  *
4  * Copyright 1996 Marcus Meissner
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winternl.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "wine/unicode.h"
33
34 #include "heap.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(resource);
38
39
40 /* Messages...used by FormatMessage32* (KERNEL32.something)
41  *
42  * They can be specified either directly or using a message ID and
43  * loading them from the resource.
44  *
45  * The resourcedata has following format:
46  * start:
47  * 0: DWORD nrofentries
48  * nrofentries * subentry:
49  *      0: DWORD firstentry
50  *      4: DWORD lastentry
51  *      8: DWORD offset from start to the stringentries
52  *
53  * (lastentry-firstentry) * stringentry:
54  * 0: WORD len (0 marks end)    [ includes the 4 byte header length ]
55  * 2: WORD flags
56  * 4: CHAR[len-4]
57  *      (stringentry i of a subentry refers to the ID 'firstentry+i')
58  *
59  * Yes, ANSI strings in win32 resources. Go figure.
60  */
61
62 /**********************************************************************
63  *      load_messageA           (internal)
64  */
65 static INT load_messageA( HMODULE instance, UINT id, WORD lang,
66                           LPSTR buffer, INT buflen )
67 {
68     const MESSAGE_RESOURCE_ENTRY *mre;
69     int         i,slen;
70
71     TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
72
73     if (RtlFindMessage( instance, (ULONG)RT_MESSAGETABLEW, lang, id, &mre ) != STATUS_SUCCESS) return 0;
74
75     slen=mre->Length;
76     TRACE("     - strlen=%d\n",slen);
77     i = min(buflen - 1, slen);
78     if (buffer == NULL)
79         return slen;
80     if (i>0) {
81         if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
82             WideCharToMultiByte( CP_ACP, 0, (LPWSTR)mre->Text, -1, buffer, i, NULL, NULL );
83         else
84             lstrcpynA(buffer, (LPSTR)mre->Text, i);
85         buffer[i]=0;
86     } else {
87         if (buflen>1) {
88             buffer[0]=0;
89             return 0;
90         }
91     }
92     if (buffer)
93             TRACE("'%s' copied !\n", buffer);
94     return i;
95 }
96
97 #if 0  /* FIXME */
98 /**********************************************************************
99  *      load_messageW   (internal)
100  */
101 static INT load_messageW( HMODULE instance, UINT id, WORD lang,
102                           LPWSTR buffer, INT buflen )
103 {
104     INT retval;
105     LPSTR buffer2 = NULL;
106     if (buffer && buflen)
107         buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
108     retval = load_messageA(instance,id,lang,buffer2,buflen);
109     if (buffer)
110     {
111         if (retval) {
112             lstrcpynAtoW( buffer, buffer2, buflen );
113             retval = strlenW( buffer );
114         }
115         HeapFree( GetProcessHeap(), 0, buffer2 );
116     }
117     return retval;
118 }
119 #endif
120
121
122 /***********************************************************************
123  *           FormatMessageA   (KERNEL32.@)
124  * FIXME: missing wrap,
125  */
126 DWORD WINAPI FormatMessageA(
127         DWORD   dwFlags,
128         LPCVOID lpSource,
129         DWORD   dwMessageId,
130         DWORD   dwLanguageId,
131         LPSTR   lpBuffer,
132         DWORD   nSize,
133         va_list* _args )
134 {
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 */
138     LPSTR       target,t;
139     DWORD       talloced;
140     LPSTR       from,f;
141     DWORD       width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
142     BOOL    eos = FALSE;
143     INT bufsize;
144     HMODULE     hmodule = (HMODULE)lpSource;
145     CHAR        ch;
146
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;
152
153     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
154         FIXME("line wrapping (%lu) not supported.\n", width);
155     from = NULL;
156     if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
157     {
158         from = HeapAlloc( GetProcessHeap(), 0, strlen((LPSTR)lpSource)+1 );
159         strcpy( from, (LPSTR)lpSource );
160     }
161     else {
162         bufsize = 0;
163
164         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
165         {
166            bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
167         }
168         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) && (!bufsize))
169         {
170            hmodule = GetModuleHandleA("kernel32");
171            bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
172         }
173
174         if (!bufsize) {
175             SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
176             return 0;
177         }
178  
179         from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
180         load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
181     }
182     target      = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
183     t   = target;
184     talloced= 100;
185
186 #define ADD_TO_T(c) do { \
187         *t++=c;\
188         if (t-target == talloced) {\
189             target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
190             t = target+talloced;\
191             talloced*=2;\
192       }\
193 } while (0)
194
195     if (from) {
196         f=from;
197         if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
198             while (*f && !eos)
199                 ADD_TO_T(*f++);
200         }
201         else {
202             while (*f && !eos) {
203                 if (*f=='%') {
204                     int insertnr;
205                     char *fmtstr,*x,*lastf;
206                     DWORD *argliststart;
207
208                     fmtstr = NULL;
209                     lastf = f;
210                     f++;
211                     if (!*f) {
212                         ADD_TO_T('%');
213                         continue;
214                     }
215                     switch (*f) {
216                     case '1':case '2':case '3':case '4':case '5':
217                     case '6':case '7':case '8':case '9':
218                         insertnr=*f-'0';
219                         switch (f[1]) {
220                         case '0':case '1':case '2':case '3':
221                         case '4':case '5':case '6':case '7':
222                         case '8':case '9':
223                             f++;
224                             insertnr=insertnr*10+*f-'0';
225                             f++;
226                             break;
227                         default:
228                             f++;
229                             break;
230                         }
231                         if (*f=='!') {
232                             f++;
233                             if (NULL!=(x=strchr(f,'!'))) {
234                                 *x='\0';
235                                 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
236                                 sprintf(fmtstr,"%%%s",f);
237                                 f=x+1;
238                             } else {
239                                 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
240                                 sprintf(fmtstr,"%%%s",f);
241                                 f+=strlen(f); /*at \0*/
242                             }
243                         } else {
244                             if(!args) break;
245                             fmtstr = HeapAlloc(GetProcessHeap(),0,3);
246                             strcpy( fmtstr, "%s" );
247                         }
248                         if (args) {
249                             int sz;
250                             LPSTR b;
251
252                             if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
253                                 argliststart=args+insertnr-1;
254                             else
255                                 argliststart=(*(DWORD**)args)+insertnr-1;
256
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);
262                             } else {
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);
267                             }
268                             for (x=b; *x; x++) ADD_TO_T(*x);
269
270                             HeapFree(GetProcessHeap(),0,b);
271                         } else {
272                                 /* NULL args - copy formatstr
273                                  * (probably wrong)
274                                  */
275                             while ((lastf<f)&&(*lastf)) {
276                                 ADD_TO_T(*lastf++);
277                             }
278                         }
279                         HeapFree(GetProcessHeap(),0,fmtstr);
280                         break;
281                     case 'n':
282                         ADD_TO_T('\r');
283                         ADD_TO_T('\n');
284                         f++;
285                         break;
286                     case '0':
287                         eos = TRUE;
288                         f++;
289                         break;
290                     default:
291                         ADD_TO_T(*f++);
292                         break;
293                     }
294                 } else {
295                     ch = *f;
296                     f++;
297                     if (ch == '\r') {
298                         if (*f == '\n')
299                             f++;
300                         if(width)
301                             ADD_TO_T(' ');
302                         else
303                         {
304                             ADD_TO_T('\r');
305                             ADD_TO_T('\n');
306                         }
307                     } else {
308                         if (ch == '\n')
309                         {
310                             if(width)
311                                 ADD_TO_T(' ');
312                             else
313                             {
314                                 ADD_TO_T('\r');
315                                 ADD_TO_T('\n');
316                             }
317                         }
318                         else
319                             ADD_TO_T(ch);
320                     }
321                 }
322             }
323         }
324         *t='\0';
325     }
326     talloced = strlen(target)+1;
327     if (nSize && talloced<nSize) {
328         target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
329     }
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);
334     } else {
335         lstrcpynA(lpBuffer,target,nSize);
336     }
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):
342             strlen(lpBuffer);
343 #else
344     return 0;
345 #endif /* __i386__ */
346 }
347 #undef ADD_TO_T
348
349
350 /***********************************************************************
351  *           FormatMessageW   (KERNEL32.@)
352  */
353 DWORD WINAPI FormatMessageW(
354         DWORD   dwFlags,
355         LPCVOID lpSource,
356         DWORD   dwMessageId,
357         DWORD   dwLanguageId,
358         LPWSTR  lpBuffer,
359         DWORD   nSize,
360         va_list* _args )
361 {
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 */
365     LPSTR target,t;
366     DWORD talloced;
367     LPSTR from,f;
368     DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
369     BOOL eos = FALSE;
370     INT bufsize;
371     HMODULE hmodule = (HMODULE)lpSource;
372     CHAR ch;
373
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;
379
380     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
381         FIXME("line wrapping not supported.\n");
382     from = NULL;
383     if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
384         from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
385     }
386     else {
387         bufsize = 0;
388
389         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
390         {
391            bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
392         }
393         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) && (!bufsize))
394         {
395            hmodule = GetModuleHandleA("kernel32");
396            bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
397         }
398
399         if (!bufsize) {
400             SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
401             return 0;
402         }
403  
404         from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
405         load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
406     }
407     target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
408     t = target;
409     talloced= 100;
410
411 #define ADD_TO_T(c)  do {\
412     *t++=c;\
413     if (t-target == talloced) {\
414         target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
415         t = target+talloced;\
416         talloced*=2;\
417     } \
418 } while (0)
419
420     if (from) {
421         f=from;
422         if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
423             while (*f && !eos)
424                 ADD_TO_T(*f++);
425         }
426         else {
427             while (*f && !eos) {
428                 if (*f=='%') {
429                     int insertnr;
430                     char *fmtstr,*sprintfbuf,*x;
431                     DWORD *argliststart;
432
433                     fmtstr = NULL;
434                     f++;
435                     if (!*f) {
436                         ADD_TO_T('%');
437                         continue;
438                     }
439
440                     switch (*f) {
441                     case '1':case '2':case '3':case '4':case '5':
442                     case '6':case '7':case '8':case '9':
443                         insertnr=*f-'0';
444                         switch (f[1]) {
445                         case '0':case '1':case '2':case '3':
446                         case '4':case '5':case '6':case '7':
447                         case '8':case '9':
448                             f++;
449                             insertnr=insertnr*10+*f-'0';
450                             f++;
451                             break;
452                         default:
453                             f++;
454                             break;
455                         }
456                         if (*f=='!') {
457                             f++;
458                             if (NULL!=(x=strchr(f,'!'))) {
459                                 *x='\0';
460                                 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
461                                 sprintf(fmtstr,"%%%s",f);
462                                 f=x+1;
463                             } else {
464                                 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
465                                 sprintf(fmtstr,"%%%s",f);
466                                 f+=strlen(f); /*at \0*/
467                             }
468                         } else {
469                             if(!args) break;
470                             fmtstr = HeapAlloc( GetProcessHeap(),0,3);
471                             strcpy( fmtstr, "%s" );
472                         }
473                         if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
474                             argliststart=args+insertnr-1;
475                         else
476                             argliststart=(*(DWORD**)args)+insertnr-1;
477
478                         if (fmtstr[strlen(fmtstr)-1]=='s' && argliststart[0]) {
479                             DWORD xarr[3];
480
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);
486
487                             /* CMF - This makes a BIG assumption about va_list */
488                             vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
489                             HeapFree(GetProcessHeap(), 0, (LPVOID) xarr[0]);
490                         } else {
491                             sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
492
493                             /* CMF - This makes a BIG assumption about va_list */
494                             vsprintf(sprintfbuf, fmtstr, (va_list) argliststart);
495                         }
496                         x=sprintfbuf;
497                         while (*x) {
498                             ADD_TO_T(*x++);
499                         }
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;
515                     }
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
525                         {
526                             ADD_TO_T('\r');
527                             ADD_TO_T('\n');
528                         }
529                     } else {
530                         if (ch == '\n')
531                         {
532                             if(width)
533                                 ADD_TO_T(' ');
534                             else
535                             {
536                                 ADD_TO_T('\r');
537                                 ADD_TO_T('\n');
538                             }
539                         }
540                         else
541                             ADD_TO_T(ch);
542                     }
543                 }
544             }
545         }
546         *t='\0';
547     }
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 );
556     }
557     else
558     {
559         if (nSize > 0 && !MultiByteToWideChar( CP_ACP, 0, target, -1, lpBuffer, nSize ))
560             lpBuffer[nSize-1] = 0;
561     }
562     HeapFree(GetProcessHeap(),0,target);
563     if (from) HeapFree(GetProcessHeap(),0,from);
564     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
565         strlenW(*(LPWSTR*)lpBuffer):
566             strlenW(lpBuffer);
567 #else
568     return 0;
569 #endif /* __i386__ */
570 }
571 #undef ADD_TO_T