wined3d: Ignore set_priority calls on non-managed resources.
[wine.git] / dlls / kernel32 / format_msg.c
blobaee7d7a15aec1aa663cb7293950fc35abbe4e179
1 /*
2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
5 * Copyright 2009 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winternl.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "wine/unicode.h"
37 #include "kernel_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(resource);
42 struct format_args
44 ULONG_PTR *args;
45 __ms_va_list *list;
46 int last;
49 /* Messages used by FormatMessage
51 * They can be specified either directly or using a message ID and
52 * loading them from the resource.
54 * The resourcedata has following format:
55 * start:
56 * 0: DWORD nrofentries
57 * nrofentries * subentry:
58 * 0: DWORD firstentry
59 * 4: DWORD lastentry
60 * 8: DWORD offset from start to the stringentries
62 * (lastentry-firstentry) * stringentry:
63 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
64 * 2: WORD flags
65 * 4: CHAR[len-4]
66 * (stringentry i of a subentry refers to the ID 'firstentry+i')
68 * Yes, ANSI strings in win32 resources. Go figure.
71 static const WCHAR FMTWSTR[] = { '%','s',0 };
73 /**********************************************************************
74 * load_message (internal)
76 static LPWSTR load_message( HMODULE module, UINT id, WORD lang )
78 const MESSAGE_RESOURCE_ENTRY *mre;
79 WCHAR *buffer;
80 NTSTATUS status;
82 TRACE("module = %p, id = %08x\n", module, id );
84 if (!module) module = GetModuleHandleW( NULL );
85 if ((status = RtlFindMessage( module, RT_MESSAGETABLE, lang, id, &mre )) != STATUS_SUCCESS)
87 SetLastError( RtlNtStatusToDosError(status) );
88 return NULL;
91 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
93 int len = (strlenW( (const WCHAR *)mre->Text ) + 1) * sizeof(WCHAR);
94 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
95 memcpy( buffer, mre->Text, len );
97 else
99 int len = MultiByteToWideChar( CP_ACP, 0, (const char *)mre->Text, -1, NULL, 0 );
100 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
101 MultiByteToWideChar( CP_ACP, 0, (const char*)mre->Text, -1, buffer, len );
103 TRACE("returning %s\n", wine_dbgstr_w(buffer));
104 return buffer;
107 /**********************************************************************
108 * get_arg (internal)
110 static ULONG_PTR get_arg( int nr, DWORD flags, struct format_args *args )
112 if (nr == -1) nr = args->last + 1;
113 if (args->list)
115 if (!args->args) args->args = HeapAlloc( GetProcessHeap(), 0, 99 * sizeof(ULONG_PTR) );
116 while (nr > args->last)
117 args->args[args->last++] = va_arg( *args->list, ULONG_PTR );
119 if (nr > args->last) args->last = nr;
120 return args->args[nr - 1];
123 /**********************************************************************
124 * format_insert (internal)
126 static LPCWSTR format_insert( BOOL unicode_caller, int insert, LPCWSTR format,
127 DWORD flags, struct format_args *args,
128 LPWSTR *result )
130 static const WCHAR fmt_u[] = {'%','u',0};
131 WCHAR *wstring = NULL, *p, fmt[256];
132 ULONG_PTR arg;
133 int size;
135 if (*format != '!') /* simple string */
137 arg = get_arg( insert, flags, args );
138 if (unicode_caller || !arg)
140 static const WCHAR nullW[] = {'(','n','u','l','l',')',0};
141 const WCHAR *str = (const WCHAR *)arg;
143 if (!str) str = nullW;
144 *result = HeapAlloc( GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR) );
145 strcpyW( *result, str );
147 else
149 const char *str = (const char *)arg;
150 DWORD length = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
151 *result = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
152 MultiByteToWideChar( CP_ACP, 0, str, -1, *result, length );
154 return format;
157 format++;
158 p = fmt;
159 *p++ = '%';
161 while (*format == '0' ||
162 *format == '+' ||
163 *format == '-' ||
164 *format == ' ' ||
165 *format == '*' ||
166 *format == '#')
168 if (*format == '*')
170 p += sprintfW( p, fmt_u, get_arg( insert, flags, args ));
171 insert = -1;
172 format++;
174 else *p++ = *format++;
176 while (isdigitW(*format)) *p++ = *format++;
178 if (*format == '.')
180 *p++ = *format++;
181 if (*format == '*')
183 p += sprintfW( p, fmt_u, get_arg( insert, flags, args ));
184 insert = -1;
185 format++;
187 else
188 while (isdigitW(*format)) *p++ = *format++;
191 /* replicate MS bug: drop an argument when using va_list with width/precision */
192 if (insert == -1 && args->list) args->last--;
193 arg = get_arg( insert, flags, args );
195 /* check for ascii string format */
196 if ((format[0] == 'h' && format[1] == 's') ||
197 (format[0] == 'h' && format[1] == 'S') ||
198 (unicode_caller && format[0] == 'S') ||
199 (!unicode_caller && format[0] == 's'))
201 DWORD len = MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, NULL, 0 );
202 wstring = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
203 MultiByteToWideChar( CP_ACP, 0, (char *)arg, -1, wstring, len );
204 arg = (ULONG_PTR)wstring;
205 *p++ = 's';
207 /* check for ascii character format */
208 else if ((format[0] == 'h' && format[1] == 'c') ||
209 (format[0] == 'h' && format[1] == 'C') ||
210 (unicode_caller && format[0] == 'C') ||
211 (!unicode_caller && format[0] == 'c'))
213 char ch = arg;
214 wstring = HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(WCHAR) );
215 MultiByteToWideChar( CP_ACP, 0, &ch, 1, wstring, 1 );
216 wstring[1] = 0;
217 arg = (ULONG_PTR)wstring;
218 *p++ = 's';
220 /* check for wide string format */
221 else if ((format[0] == 'l' && format[1] == 's') ||
222 (format[0] == 'l' && format[1] == 'S') ||
223 (format[0] == 'w' && format[1] == 's') ||
224 (!unicode_caller && format[0] == 'S'))
226 *p++ = 's';
228 /* check for wide character format */
229 else if ((format[0] == 'l' && format[1] == 'c') ||
230 (format[0] == 'l' && format[1] == 'C') ||
231 (format[0] == 'w' && format[1] == 'c') ||
232 (!unicode_caller && format[0] == 'C'))
234 *p++ = 'c';
236 /* FIXME: handle I64 etc. */
237 else while (*format && *format != '!') *p++ = *format++;
239 *p = 0;
240 size = 256;
241 for (;;)
243 WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
244 int needed = snprintfW( ret, size, fmt, arg );
245 if (needed == -1 || needed >= size)
247 HeapFree( GetProcessHeap(), 0, ret );
248 size = max( needed + 1, size * 2 );
250 else
252 *result = ret;
253 break;
257 while (*format && *format != '!') format++;
258 if (*format == '!') format++;
260 HeapFree( GetProcessHeap(), 0, wstring );
261 return format;
264 struct _format_message_data
266 LPWSTR formatted;
267 DWORD size;
268 LPWSTR t;
269 LPWSTR space;
270 BOOL inspace;
271 DWORD width, w;
274 static void format_add_char(struct _format_message_data *fmd, WCHAR c)
276 *fmd->t++ = c;
277 if (fmd->width && fmd->width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
279 switch (c) {
280 case '\r':
281 case '\n':
282 fmd->space = NULL;
283 fmd->inspace = FALSE;
284 fmd->w = 0;
285 break;
286 case ' ':
287 if (!fmd->inspace)
288 fmd->space = fmd->t - 1;
289 fmd->inspace = TRUE;
290 fmd->w++;
291 break;
292 default:
293 fmd->inspace = FALSE;
294 fmd->w++;
296 if (fmd->w == fmd->width) {
297 LPWSTR notspace;
298 if (fmd->space) {
299 notspace = fmd->space;
300 while (notspace != fmd->t && *notspace == ' ')
301 notspace++;
302 } else
303 notspace = fmd->space = fmd->t;
304 fmd->w = fmd->t - notspace;
305 memmove(fmd->space+2, notspace, fmd->w * sizeof(*fmd->t));
306 *fmd->space++ = '\r';
307 *fmd->space++ = '\n';
308 fmd->t = fmd->space + fmd->w;
309 fmd->space = NULL;
310 fmd->inspace = FALSE;
313 if ((DWORD)(fmd->t - fmd->formatted) == fmd->size) {
314 DWORD_PTR ispace = fmd->space - fmd->formatted;
315 /* Allocate two extra characters so we can insert a '\r\n' in
316 * the middle of a word.
318 fmd->formatted = HeapReAlloc(GetProcessHeap(), 0, fmd->formatted, (fmd->size * 2 + 2) * sizeof(WCHAR));
319 fmd->t = fmd->formatted + fmd->size;
320 if (fmd->space)
321 fmd->space = fmd->formatted + ispace;
322 fmd->size *= 2;
326 /**********************************************************************
327 * format_message (internal)
329 static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr,
330 struct format_args *format_args )
332 struct _format_message_data fmd;
333 LPCWSTR f;
334 BOOL eos = FALSE;
336 fmd.size = 100;
337 fmd.formatted = fmd.t = HeapAlloc( GetProcessHeap(), 0, (fmd.size + 2) * sizeof(WCHAR) );
339 fmd.width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
340 fmd.w = 0;
341 fmd.inspace = FALSE;
342 fmd.space = NULL;
343 f = fmtstr;
344 while (*f && !eos) {
345 if (*f=='%') {
346 int insertnr;
347 WCHAR *str,*x;
349 f++;
350 switch (*f) {
351 case '1':case '2':case '3':case '4':case '5':
352 case '6':case '7':case '8':case '9':
353 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS)
354 goto ignore_inserts;
355 else if (((dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) && !format_args->args) ||
356 (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) && !format_args->list))
358 SetLastError(ERROR_INVALID_PARAMETER);
359 HeapFree(GetProcessHeap(), 0, fmd.formatted);
360 return NULL;
362 insertnr = *f-'0';
363 switch (f[1]) {
364 case '0':case '1':case '2':case '3':
365 case '4':case '5':case '6':case '7':
366 case '8':case '9':
367 f++;
368 insertnr = insertnr*10 + *f-'0';
369 f++;
370 break;
371 default:
372 f++;
373 break;
375 f = format_insert( unicode_caller, insertnr, f, dwFlags, format_args, &str );
376 for (x = str; *x; x++) format_add_char(&fmd, *x);
377 HeapFree( GetProcessHeap(), 0, str );
378 break;
379 case 'n':
380 format_add_char(&fmd, '\r');
381 format_add_char(&fmd, '\n');
382 f++;
383 break;
384 case 'r':
385 format_add_char(&fmd, '\r');
386 f++;
387 break;
388 case 't':
389 format_add_char(&fmd, '\t');
390 f++;
391 break;
392 case '0':
393 eos = TRUE;
394 f++;
395 break;
396 case '\0':
397 SetLastError(ERROR_INVALID_PARAMETER);
398 HeapFree(GetProcessHeap(), 0, fmd.formatted);
399 return NULL;
400 ignore_inserts:
401 default:
402 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS)
403 format_add_char(&fmd, '%');
404 format_add_char(&fmd, *f++);
405 break;
407 } else {
408 WCHAR ch = *f;
409 f++;
410 if (ch == '\r') {
411 if (*f == '\n')
412 f++;
413 if(fmd.width)
414 format_add_char(&fmd, ' ');
415 else
417 format_add_char(&fmd, '\r');
418 format_add_char(&fmd, '\n');
420 } else {
421 if (ch == '\n')
423 if(fmd.width)
424 format_add_char(&fmd, ' ');
425 else
427 format_add_char(&fmd, '\r');
428 format_add_char(&fmd, '\n');
431 else
432 format_add_char(&fmd, ch);
436 *fmd.t = '\0';
438 return fmd.formatted;
441 /***********************************************************************
442 * FormatMessageA (KERNEL32.@)
444 DWORD WINAPI FormatMessageA(
445 DWORD dwFlags,
446 LPCVOID lpSource,
447 DWORD dwMessageId,
448 DWORD dwLanguageId,
449 LPSTR lpBuffer,
450 DWORD nSize,
451 __ms_va_list* args )
453 struct format_args format_args;
454 DWORD ret = 0;
455 LPWSTR target;
456 DWORD destlength;
457 LPWSTR from;
459 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
460 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
462 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
464 if (!lpBuffer)
466 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
467 return 0;
469 else
470 *(LPSTR *)lpBuffer = NULL;
473 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
475 format_args.args = (ULONG_PTR *)args;
476 format_args.list = NULL;
477 format_args.last = 0;
479 else
481 format_args.args = NULL;
482 format_args.list = args;
483 format_args.last = 0;
486 from = NULL;
487 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
489 DWORD length = MultiByteToWideChar(CP_ACP, 0, lpSource, -1, NULL, 0);
490 from = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
491 MultiByteToWideChar(CP_ACP, 0, lpSource, -1, from, length);
493 else if (dwFlags & (FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM))
495 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
496 from = load_message( (HMODULE)lpSource, dwMessageId, dwLanguageId );
497 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
498 from = load_message( kernel32_handle, dwMessageId, dwLanguageId );
499 if (!from) return 0;
501 else
503 SetLastError(ERROR_INVALID_PARAMETER);
504 return 0;
507 target = format_message( FALSE, dwFlags, from, &format_args );
508 if (!target)
509 goto failure;
511 TRACE("-- %s\n", debugstr_w(target));
513 /* Only try writing to an output buffer if there are processed characters
514 * in the temporary output buffer. */
515 if (*target)
517 destlength = WideCharToMultiByte(CP_ACP, 0, target, -1, NULL, 0, NULL, NULL);
518 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
520 LPSTR buf = LocalAlloc(LMEM_ZEROINIT, max(nSize, destlength));
521 WideCharToMultiByte(CP_ACP, 0, target, -1, buf, destlength, NULL, NULL);
522 *((LPSTR*)lpBuffer) = buf;
524 else
526 if (nSize < destlength)
528 SetLastError(ERROR_INSUFFICIENT_BUFFER);
529 goto failure;
531 WideCharToMultiByte(CP_ACP, 0, target, -1, lpBuffer, destlength, NULL, NULL);
533 ret = destlength - 1; /* null terminator */
536 failure:
537 HeapFree(GetProcessHeap(),0,target);
538 HeapFree(GetProcessHeap(),0,from);
539 if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
540 TRACE("-- returning %u\n", ret);
541 return ret;
544 /***********************************************************************
545 * FormatMessageW (KERNEL32.@)
547 DWORD WINAPI FormatMessageW(
548 DWORD dwFlags,
549 LPCVOID lpSource,
550 DWORD dwMessageId,
551 DWORD dwLanguageId,
552 LPWSTR lpBuffer,
553 DWORD nSize,
554 __ms_va_list* args )
556 struct format_args format_args;
557 DWORD ret = 0;
558 LPWSTR target;
559 DWORD talloced;
560 LPWSTR from;
562 TRACE("(0x%x,%p,%d,0x%x,%p,%d,%p)\n",
563 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
565 if (!lpBuffer)
567 SetLastError(ERROR_INVALID_PARAMETER);
568 return 0;
571 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
572 *(LPWSTR *)lpBuffer = NULL;
574 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
576 format_args.args = (ULONG_PTR *)args;
577 format_args.list = NULL;
578 format_args.last = 0;
580 else
582 format_args.args = NULL;
583 format_args.list = args;
584 format_args.last = 0;
587 from = NULL;
588 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
589 from = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpSource) + 1) *
590 sizeof(WCHAR) );
591 strcpyW( from, lpSource );
593 else if (dwFlags & (FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM))
595 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
596 from = load_message( (HMODULE)lpSource, dwMessageId, dwLanguageId );
597 if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
598 from = load_message( kernel32_handle, dwMessageId, dwLanguageId );
599 if (!from) return 0;
601 else
603 SetLastError(ERROR_INVALID_PARAMETER);
604 return 0;
607 target = format_message( TRUE, dwFlags, from, &format_args );
608 if (!target)
609 goto failure;
611 talloced = strlenW(target)+1;
612 TRACE("-- %s\n",debugstr_w(target));
614 /* Only allocate a buffer if there are processed characters in the
615 * temporary output buffer. If a caller supplies the buffer, then
616 * a null terminator will be written to it. */
617 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
619 if (*target)
621 /* nSize is the MINIMUM size */
622 *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT, max(nSize, talloced)*sizeof(WCHAR));
623 strcpyW(*(LPWSTR*)lpBuffer, target);
626 else
628 if (nSize < talloced)
630 SetLastError(ERROR_INSUFFICIENT_BUFFER);
631 goto failure;
633 strcpyW(lpBuffer, target);
636 ret = talloced - 1; /* null terminator */
637 failure:
638 HeapFree(GetProcessHeap(),0,target);
639 HeapFree(GetProcessHeap(),0,from);
640 if (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)) HeapFree( GetProcessHeap(), 0, format_args.args );
641 TRACE("-- returning %u\n", ret);
642 return ret;