ntdll: Don't try to map the PE file past the size of the backing file.
[wine.git] / programs / dxdiag / main.c
blob4533236f0f5e39afd1e5cea3ef3b7ce1bfc54d18
1 /*
2 * DxDiag Implementation
4 * Copyright 2009 Austin English
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 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23 #include <dxdiag.h>
25 #include "wine/debug.h"
26 #include "dxdiag_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
30 HINSTANCE hInstance;
32 struct command_line_info
34 WCHAR outfile[MAX_PATH];
35 enum output_type output_type;
36 BOOL whql_check;
39 static void usage(void)
41 WCHAR title[MAX_STRING_LEN];
42 WCHAR usage[MAX_STRING_LEN];
44 LoadStringW(hInstance, STRING_DXDIAG_TOOL, title, ARRAY_SIZE(title));
45 LoadStringW(hInstance, STRING_USAGE, usage, ARRAY_SIZE(usage));
47 MessageBoxW(NULL, usage, title, MB_OK | MB_ICONWARNING);
49 ExitProcess(0);
52 static BOOL process_file_name(const WCHAR *cmdline, enum output_type output_type, WCHAR *filename, size_t filename_len)
54 const WCHAR *endptr;
55 size_t len;
57 /* Skip any intervening spaces. */
58 while (*cmdline == ' ')
59 cmdline++;
61 /* Ignore filename quoting, if any. */
62 if (*cmdline == '"' && (endptr = wcsrchr(cmdline, '"')))
64 /* Reject a string with only one quote. */
65 if (cmdline == endptr)
66 return FALSE;
68 cmdline++;
70 else
71 endptr = cmdline + lstrlenW(cmdline);
73 len = endptr - cmdline;
74 if (len == 0 || len >= filename_len)
75 return FALSE;
77 memcpy(filename, cmdline, len * sizeof(WCHAR));
78 filename[len] = '\0';
80 /* Append an extension appropriate for the output type if the filename does not have one. */
81 if (!wcsrchr(filename, '.'))
83 const WCHAR *filename_ext = get_output_extension(output_type);
85 if (len + lstrlenW(filename_ext) >= filename_len)
86 return FALSE;
88 lstrcatW(filename, filename_ext);
91 return TRUE;
95 Process options [/WHQL:ON|OFF][/X outfile|/T outfile]
96 Returns TRUE if options were present, FALSE otherwise
97 Only one of /X and /T is allowed, /WHQL must come before /X and /T,
98 and the rest of the command line after /X or /T is interpreted as a
99 filename. If a non-option portion of the command line is encountered,
100 dxdiag assumes that the string is a filename for the /T option.
102 Native does not interpret quotes, but quotes are parsed here because of how
103 Wine handles the command line.
106 static BOOL process_command_line(const WCHAR *cmdline, struct command_line_info *info)
108 static const WCHAR whql_colonW[] = {'w','h','q','l',':',0};
109 static const WCHAR offW[] = {'o','f','f',0};
110 static const WCHAR onW[] = {'o','n',0};
111 static const WCHAR dontskipW[] = {'d','o','n','t','s','k','i','p',0};
113 info->whql_check = FALSE;
114 info->output_type = OUTPUT_NONE;
116 while (*cmdline)
118 /* Skip whitespace before arg */
119 while (*cmdline == ' ')
120 cmdline++;
122 /* If no option is specified, treat the command line as a filename. */
123 if (*cmdline != '-' && *cmdline != '/')
125 info->output_type = OUTPUT_TEXT;
126 return process_file_name(cmdline, OUTPUT_TEXT, info->outfile,
127 ARRAY_SIZE(info->outfile));
130 cmdline++;
132 switch (*cmdline)
134 case 'T':
135 case 't':
136 info->output_type = OUTPUT_TEXT;
137 return process_file_name(cmdline + 1, OUTPUT_TEXT, info->outfile,
138 ARRAY_SIZE(info->outfile));
139 case 'X':
140 case 'x':
141 info->output_type = OUTPUT_XML;
142 return process_file_name(cmdline + 1, OUTPUT_XML, info->outfile,
143 ARRAY_SIZE(info->outfile));
144 case 'W':
145 case 'w':
146 if (wcsnicmp(cmdline, whql_colonW, 5))
147 return FALSE;
149 cmdline += 5;
151 if (!wcsnicmp(cmdline, offW, 3))
153 info->whql_check = FALSE;
154 cmdline += 2;
156 else if (!wcsnicmp(cmdline, onW, 2))
158 info->whql_check = TRUE;
159 cmdline++;
161 else
162 return FALSE;
164 break;
166 case 'd':
167 case 'D':
168 if (wcsnicmp(cmdline, dontskipW, 8))
169 return FALSE;
170 cmdline += 8;
171 break;
173 default:
174 return FALSE;
177 cmdline++;
180 return TRUE;
183 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
185 struct command_line_info info;
186 struct dxdiag_information *dxdiag_info;
188 hInstance = hInst;
190 if (!process_command_line(cmdline, &info))
191 usage();
193 WINE_TRACE("WHQL check: %s\n", info.whql_check ? "TRUE" : "FALSE");
194 WINE_TRACE("Output type: %d\n", info.output_type);
195 if (info.output_type != OUTPUT_NONE)
196 WINE_TRACE("Output filename: %s\n", debugstr_output_type(info.output_type));
198 CoInitialize(NULL);
200 dxdiag_info = collect_dxdiag_information(info.whql_check);
201 if (!dxdiag_info)
203 WINE_ERR("DxDiag information collection failed\n");
204 CoUninitialize();
205 return 1;
208 if (info.output_type != OUTPUT_NONE)
209 output_dxdiag_information(dxdiag_info, info.outfile, info.output_type);
210 else
211 WINE_FIXME("Information dialog is not implemented\n");
213 free_dxdiag_information(dxdiag_info);
215 CoUninitialize();
216 return 0;