1 /*
2  * Copyright (C) 2011-2020 Daniel Scharrer
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the author(s) be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #include "setup/info.hpp"
22 
23 #include <cassert>
24 #include <istream>
25 #include <sstream>
26 
27 #include <boost/foreach.hpp>
28 
29 #include "setup/component.hpp"
30 #include "setup/data.hpp"
31 #include "setup/delete.hpp"
32 #include "setup/directory.hpp"
33 #include "setup/file.hpp"
34 #include "setup/icon.hpp"
35 #include "setup/ini.hpp"
36 #include "setup/item.hpp"
37 #include "setup/language.hpp"
38 #include "setup/message.hpp"
39 #include "setup/permission.hpp"
40 #include "setup/registry.hpp"
41 #include "setup/run.hpp"
42 #include "setup/task.hpp"
43 #include "setup/type.hpp"
44 #include "stream/block.hpp"
45 #include "util/fstream.hpp"
46 #include "util/load.hpp"
47 #include "util/log.hpp"
48 #include "util/output.hpp"
49 
50 namespace setup {
51 
52 template <class Entry>
load_entries(std::istream & is,entry_types entries,size_t count,std::vector<Entry> & result,entry_types::enum_type entry_type)53 void info::load_entries(std::istream & is, entry_types entries, size_t count,
54                         std::vector<Entry> & result, entry_types::enum_type entry_type) {
55 
56 	result.clear();
57 	if(entries & entry_type) {
58 		result.resize(count);
59 		for(size_t i = 0; i < count; i++) {
60 			result[i].load(is, *this);
61 		}
62 	} else {
63 		for(size_t i = 0; i < count; i++) {
64 			Entry entry;
65 			entry.load(is, *this);
66 		}
67 	}
68 }
69 
70 namespace {
71 
load_wizard_images(std::istream & is,const setup::version & version,std::vector<std::string> & images,info::entry_types entries)72 void load_wizard_images(std::istream & is, const setup::version & version,
73                         std::vector<std::string> & images, info::entry_types entries) {
74 
75 	size_t count = 1;
76 	if(version >= INNO_VERSION(5, 6, 0)) {
77 		count = util::load<boost::uint32_t>(is);
78 	}
79 
80 	if(entries & (info::WizardImages | info::NoSkip)) {
81 		images.resize(count);
82 		for(size_t i = 0; i < count; i++) {
83 			is >> util::binary_string(images[i]);
84 		}
85 		if(version < INNO_VERSION(5, 6, 0) && images[0].empty()) {
86 			images.clear();
87 		}
88 	} else {
89 		for(size_t i = 0; i < count; i++) {
90 			util::binary_string::skip(is);
91 		}
92 	}
93 
94 }
95 
load_wizard_and_decompressor(std::istream & is,const setup::version & version,const setup::header & header,setup::info & info,info::entry_types entries)96 void load_wizard_and_decompressor(std::istream & is, const setup::version & version,
97                                   const setup::header & header,
98                                   setup::info & info, info::entry_types entries) {
99 
100 	info.wizard_images.clear();
101 	info.wizard_images_small.clear();
102 
103 	load_wizard_images(is, version, info.wizard_images, entries);
104 
105 	if(version >= INNO_VERSION(2, 0, 0) || version.is_isx()) {
106 		load_wizard_images(is, version, info.wizard_images_small, entries);
107 	}
108 
109 	info.decompressor_dll.clear();
110 	if(header.compression == stream::BZip2
111 	   || (header.compression == stream::LZMA1 && version == INNO_VERSION(4, 1, 5))
112 	   || (header.compression == stream::Zlib && version >= INNO_VERSION(4, 2, 6))) {
113 		if(entries & (info::DecompressorDll | info::NoSkip)) {
114 			is >> util::binary_string(info.decompressor_dll);
115 		} else {
116 			// decompressor dll - we don't need this
117 			util::binary_string::skip(is);
118 		}
119 	}
120 
121 	info.decrypt_dll.clear();
122 	if(header.options & header::EncryptionUsed) {
123 		if(entries & (info::DecryptDll | info::NoSkip)) {
124 			is >> util::binary_string(info.decrypt_dll);
125 		} else {
126 			// decrypt dll - we don't need this
127 			util::binary_string::skip(is);
128 		}
129 	}
130 
131 }
132 
check_is_end(stream::block_reader::pointer & is,const char * what)133 void check_is_end(stream::block_reader::pointer & is, const char * what) {
134 	is->exceptions(std::ios_base::goodbit);
135 	char dummy;
136 	if(!is->get(dummy).eof()) {
137 		throw std::ios_base::failure(what);
138 	}
139 }
140 
141 } // anonymous namespace
142 
try_load(std::istream & is,entry_types entries,util::codepage_id force_codepage)143 void info::try_load(std::istream & is, entry_types entries, util::codepage_id force_codepage) {
144 
145 	debug("trying to load setup headers for version " << version);
146 
147 	if((entries & (Messages | NoSkip)) || (!version.is_unicode() && !force_codepage)) {
148 		entries |= Languages;
149 	}
150 
151 	stream::block_reader::pointer reader = stream::block_reader::get(is, version);
152 
153 	debug("loading main header");
154 	header.load(*reader, version);
155 
156 	debug("loading languages");
157 	load_entries(*reader, entries, header.language_count, languages, Languages);
158 
159 	debug("determining encoding");
160 	if(version.is_unicode()) {
161 		// Unicode installers are always UTF16-LE, do not allow users to override that.
162 		codepage = util::cp_utf16le;
163 	} else if(force_codepage) {
164 		codepage = force_codepage;
165 	} else if(languages.empty()) {
166 		codepage = util::cp_windows1252;
167 	} else {
168 		// Non-Unicode installers do not have a defined codepage but instead just assume the
169 		// codepage of the system the installer is run on.
170 		// Look at the list of available languages to guess a suitable codepage.
171 		codepage = languages[0].codepage;
172 		BOOST_FOREACH(const language_entry & language, languages) {
173 			if(language.codepage == util::cp_windows1252) {
174 				codepage = util::cp_windows1252;
175 				break;
176 			}
177 		}
178 	}
179 
180 	header.decode(codepage);
181 	BOOST_FOREACH(language_entry & language, languages) {
182 		language.decode(codepage);
183 	}
184 
185 	if(version < INNO_VERSION(4, 0, 0)) {
186 		debug("loading images and plugins");
187 		load_wizard_and_decompressor(*reader, version, header, *this, entries);
188 	}
189 
190 	debug("loading messages");
191 	load_entries(*reader, entries, header.message_count, messages, Messages);
192 	debug("loading permissions");
193 	load_entries(*reader, entries, header.permission_count, permissions, Permissions);
194 	debug("loading types");
195 	load_entries(*reader, entries, header.type_count, types, Types);
196 	debug("loading components");
197 	load_entries(*reader, entries, header.component_count, components, Components);
198 	debug("loading tasks");
199 	load_entries(*reader, entries, header.task_count, tasks, Tasks);
200 	debug("loading directories");
201 	load_entries(*reader, entries, header.directory_count, directories, Directories);
202 	debug("loading files");
203 	load_entries(*reader, entries, header.file_count, files, Files);
204 	debug("loading icons");
205 	load_entries(*reader, entries, header.icon_count, icons, Icons);
206 	debug("loading ini entries");
207 	load_entries(*reader, entries, header.ini_entry_count, ini_entries, IniEntries);
208 	debug("loading registry entries");
209 	load_entries(*reader, entries, header.registry_entry_count, registry_entries, RegistryEntries);
210 	debug("loading delete entries");
211 	load_entries(*reader, entries, header.delete_entry_count, delete_entries, DeleteEntries);
212 	debug("loading uninstall delete entries");
213 	load_entries(*reader, entries, header.uninstall_delete_entry_count, uninstall_delete_entries,
214 	             UninstallDeleteEntries);
215 	debug("loading run entries");
216 	load_entries(*reader, entries, header.run_entry_count, run_entries, RunEntries);
217 	debug("loading uninstall run entries");
218 	load_entries(*reader, entries, header.uninstall_run_entry_count, uninstall_run_entries,
219 	             UninstallRunEntries);
220 
221 	if(version >= INNO_VERSION(4, 0, 0)) {
222 		debug("loading images and plugins");
223 		load_wizard_and_decompressor(*reader, version, header, *this, entries);
224 	}
225 
226 	// restart the compression stream
227 	check_is_end(reader, "unknown data at end of primary header stream");
228 	reader = stream::block_reader::get(is, version);
229 
230 	debug("loading data entries");
231 	load_entries(*reader, entries, header.data_entry_count, data_entries, DataEntries);
232 
233 	check_is_end(reader, "unknown data at end of secondary header stream");
234 }
235 
load(std::istream & is,entry_types entries,util::codepage_id force_codepage)236 void info::load(std::istream & is, entry_types entries, util::codepage_id force_codepage) {
237 
238 	version.load(is);
239 
240 	if(!version.known) {
241 		if(entries & NoUnknownVersion) {
242 			std::ostringstream oss;
243 			oss << "Unexpected setup data version: " << version;
244 			throw std::runtime_error(oss.str());
245 		}
246 		log_warning << "Unexpected setup data version: "
247 		            << color::white << version << color::reset;
248 	}
249 
250 	version_constant listed_version = version.value;
251 
252 	// Some setup versions didn't increment the data version number when they should have.
253 	// To work around this, we try to parse the headers for all data versions and use the first
254 	// version that parses without warnings or errors.
255 	bool ambiguous = !version.known || version.is_ambiguous();
256 	if(version.is_ambiguous()) {
257 		// Force parsing all headers so that we don't miss any errors.
258 		entries |= NoSkip;
259 	}
260 
261 	bool parsed_without_errors = false;
262 	std::streampos start = is.tellg();
263 	for(;;) {
264 
265 		warning_suppressor warnings;
266 
267 		try {
268 
269 			// Try to parse headers for this version
270 			try_load(is, entries, force_codepage);
271 
272 			if(warnings) {
273 				// Parsed without errors but with warnings - try other versions first
274 				if(!parsed_without_errors) {
275 					listed_version = version.value;
276 					parsed_without_errors = true;
277 				}
278 				throw std::exception();
279 			}
280 
281 			warnings.flush();
282 			return;
283 
284 		} catch(...) {
285 
286 			is.clear();
287 			is.seekg(start);
288 
289 			version_constant next_version = version.next();
290 
291 			if(!ambiguous || !next_version) {
292 				if(version.value != listed_version) {
293 					// Rewind to a previous version that had better results and report those
294 					version.value = listed_version;
295 					warnings.restore();
296 					try_load(is, entries, force_codepage);
297 				} else {
298 					// Otherwise. report results for the current version
299 					warnings.flush();
300 					if(!parsed_without_errors) {
301 						throw;
302 					}
303 				}
304 				return;
305 			}
306 
307 			// Retry with the next version
308 			version.value = next_version;
309 			ambiguous = version.is_ambiguous();
310 
311 		}
312 
313 	}
314 
315 }
316 
info()317 info::info() : codepage(0) { }
~info()318 info::~info() { }
319 
320 } // namespace setup
321