Debian Codegraph @

Source/icon.cpp

raw
1 /*
2 * icon.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17
18 #include "Platform.h"
19 #include "icon.h"
20 #include "util.h"
21 #include "lang.h"
22
23 #include <stdio.h>
24 #include <stdexcept>
25 #include <vector>
26 #include <algorithm>
27
28 using namespace std;
29
30 extern int g_display_errors;
31 extern FILE *g_output;
32
33 #define SIZEOF_RSRC_ICON_GROUP_ENTRY 14
34 enum { ICO_TYPE_ICON = 1, ICO_TYPE_CURSOR = 2 };
35
36 static bool is_valid_header(const void*headerdata)
37 {
38 assert(sizeof(IconGroupHeader) == 6);
39 IconGroupHeader*pGH = (IconGroupHeader*) headerdata;
40 if (pGH->wReserved != FIX_ENDIAN_INT16(0x0000)) return false;
41 WORD type = FIX_ENDIAN_INT16(pGH->wType);
42 if (type != ICO_TYPE_ICON && type != ICO_TYPE_CURSOR) return false;
43 return FIX_ENDIAN_INT16(pGH->wCount) != 0;
44 }
45
46 template<class S> static WORD read_icon_header(S*f, IconGroupHeader& igh)
47 {
48 if (!freadall(&igh, sizeof(IconGroupHeader), f))
49 {
50 fclose(f);
51 throw runtime_error("unable to read header from file");
52 }
53
54 if (!is_valid_header(&igh))
55 {
56 igh.wType = igh.wCount = 0;
57 fclose(f);
58 throw runtime_error("invalid icon file");
59 }
60
61 FIX_ENDIAN_INT16_INPLACE(igh.wReserved);
62 FIX_ENDIAN_INT16_INPLACE(igh.wType);
63 FIX_ENDIAN_INT16_INPLACE(igh.wCount);
64 return igh.wCount;
65 }
66
67 void free_loaded_icon(IconGroup&icon)
68 {
69 for (IconGroup::size_type i = 0; i < icon.size(); i++)
70 delete [] icon[i].data;
71 icon.clear();
72 }
73
74 IconGroup load_icon_res(CResourceEditor* re, LPWSTR RT, WORD RN, LANGID RL)
75 {
76 IconGroupHeader* header;
77 IconGroup result;
78
79 LPBYTE group = re->GetResource(RT, RN, RL);
80 if (!group)
81 throw runtime_error("can't find icon group");
82
83 header = (IconGroupHeader*) group;
84
85 // Note: To handle cursors, use CResourceEditor::ExtractIcoCur
86 if (MAKEINTRESOURCE((size_t) RT) != RT_GROUP_ICON)
87 throw runtime_error("unsupported type");
88
89 for (WORD i = 0; i < FIX_ENDIAN_INT16(header->wCount); i++)
90 {
91 Icon icon;
92 icon.index = i;
93
94 RsrcIconGroupEntry* entry = (RsrcIconGroupEntry*) (group
95 + sizeof(IconGroupHeader) + SIZEOF_RSRC_ICON_GROUP_ENTRY * i);
96
97 memcpy(&icon.meta, &entry->header, sizeof(IconGroupEntry));
98
99 WORD rsrc_id = FIX_ENDIAN_INT16(entry->wRsrcId);
100 icon.data = re->GetResource(RT_ICON, rsrc_id, RL);
101 if (!icon.data)
102 {
103 free_loaded_icon(result);
104 throw runtime_error("can't find icon");
105 }
106
107 result.push_back(icon);
108 }
109
110 re->FreeResource(group);
111 return result;
112 }
113
114 IconGroup load_icon_res(CResourceEditor* re, WORD id)
115 {
116 return load_icon_res(re, RT_GROUP_ICON, id, NSIS_DEFAULT_LANG);
117 }
118
119 template<class S> static IconGroup load_iconimages_from_stream(const IconGroupHeader&iconHeader, S&file)
120 {
121 IconGroup result;
122
123 for (WORD i = 0; i < iconHeader.wCount; i++)
124 {
125 Icon icon;
126 icon.index = i;
127 icon.data = NULL;
128
129 if (!freadall(&icon.meta, sizeof(IconGroupEntry), file))
130 {
131 free_loaded_icon(result);
132 throw runtime_error("unable to read entry from file");
133 }
134
135 DWORD size = FIX_ENDIAN_INT32(icon.meta.dwRawSize);
136 if (size > 1048576) // magic numbers are great
137 {
138 free_loaded_icon(result);
139 throw runtime_error("invalid icon file size");
140 }
141
142 DWORD iconOffset;
143 if (!freadall(&iconOffset, sizeof(DWORD), file))
144 {
145 free_loaded_icon(result);
146 throw runtime_error("unable to read offset from file");
147 }
148
149 FIX_ENDIAN_INT32_INPLACE(iconOffset);
150
151 fpos_t pos;
152 fgetpos(file, &pos);
153 if (fseek(file, iconOffset, SEEK_SET))
154 {
155 free_loaded_icon(result);
156 throw runtime_error("corrupted icon file, too small");
157 }
158
159 icon.data = new BYTE[size];
160 if (!freadall(icon.data, size, file)) die:
161 {
162 free_loaded_icon(result);
163 throw runtime_error("unable to read icon from file");
164 }
165
166 if (fsetpos(file, &pos))
167 goto die;
168
169 result.push_back(icon);
170 }
171
172 return result;
173 }
174
175 template<class S> static IconGroup load_icon_from_stream(S*file)
176 {
177 IconGroupHeader iconHeader;
178 read_icon_header(file, iconHeader);
179 return load_iconimages_from_stream(iconHeader, file);
180 }
181
182 IconGroup load_icon_file(const TCHAR* filename)
183 {
184 FILE* f = FOPEN(filename, ("rb"));
185 if (!f)
186 throw runtime_error("can't open file");
187
188 MANAGE_WITH(f, fclose);
189 return load_icon_from_stream(f);
190 }
191
192 IconGroup load_icon(const TCHAR* filename)
193 {
194 if (CResourceEditor::IsResProtocol(filename)) // Try opening embedded resource
195 {
196 CResourceEditor::EXTERNAL x;
197 if (CResourceEditor::MapExternal(filename, CResourceEditor::TM_ICON, x))
198 {
199 CStdFileStreamOnMemory stream(x.Data, x.cbData);
200 IconGroup icon = load_icon_from_stream(&stream);
201 CResourceEditor::FreeExternal(x);
202 return icon;
203 }
204 }
205 return load_icon_file(filename);
206 }
207
208 typedef struct
209 {
210 unsigned index1;
211 unsigned index2;
212 DWORD size;
213 unsigned size_index;
214 } IconPair;
215
216
217 typedef vector<IconPair> IconPairs;
218
219 static bool compare_icon(Icon a, Icon b)
220 {
221 return FIX_ENDIAN_INT32(a.meta.dwRawSize) > FIX_ENDIAN_INT32(b.meta.dwRawSize);
222 }
223
224 static IconGroup sort_icon(IconGroup icon)
225 {
226 IconGroup sorted = icon;
227 sort(sorted.begin(), sorted.end(), compare_icon);
228 return sorted;
229 }
230
231 static bool compare_pairs_index1(IconPair a, IconPair b)
232 {
233 return a.index1 < b.index1;
234 }
235
236 static bool compare_pairs_index2(IconPair a, IconPair b)
237 {
238 return a.index2 < b.index2;
239 }
240
241 static IconPairs sort_pairs(IconPairs pairs, bool first)
242 {
243 IconPairs sorted = pairs;
244 sort(sorted.begin(), sorted.end(), first ? compare_pairs_index1 : compare_pairs_index2);
245 return sorted;
246 }
247
248 static IconPairs get_icon_order(IconGroup icon1, IconGroup icon2)
249 {
250 IconGroup sorted_icons1 = sort_icon(icon1);
251 IconGroup sorted_icons2 = sort_icon(icon2);
252
253 IconGroup::size_type shared_count = min(sorted_icons1.size(), sorted_icons2.size());
254 IconGroup::size_type total_count = max(sorted_icons1.size(), sorted_icons2.size());
255
256 IconPairs result;
257 IconGroup::size_type i;
258
259 for (i = 0; i < shared_count; i++)
260 {
261 IconPair pair;
262
263 pair.index1 = sorted_icons1[i].index;
264 pair.index2 = sorted_icons2[i].index;
265 pair.size = max(
266 FIX_ENDIAN_INT32(sorted_icons1[i].meta.dwRawSize),
267 FIX_ENDIAN_INT32(sorted_icons2[i].meta.dwRawSize)
268 );
269 pair.size_index = truncate_cast(unsigned int,i);
270
271 result.push_back(pair);
272 }
273
274 for (; i < total_count; i++)
275 {
276 IconPair pair;
277
278 if (i < sorted_icons1.size())
279 {
280 pair.index1 = sorted_icons1[i].index;
281 pair.index2 = 0xffff;
282 pair.size = FIX_ENDIAN_INT32(sorted_icons1[i].meta.dwRawSize);
283 pair.size_index = truncate_cast(unsigned int,i);
284 }
285
286 if (i < sorted_icons2.size())
287 {
288 pair.index2 = sorted_icons2[i].index;
289 pair.index1 = 0xffff;
290 pair.size = FIX_ENDIAN_INT32(sorted_icons2[i].meta.dwRawSize);
291 pair.size_index = truncate_cast(unsigned int,i);
292 }
293
294 result.push_back(pair);
295 }
296
297 return result;
298 }
299
300 #define destroy_icon_group(p) ( delete [] (p) )
301 static LPBYTE generate_icon_group(IconGroup icon, IconPairs order, bool first)
302 {
303 size_t groupsize =
304 sizeof(IconGroupHeader) // header
305 + order.size() * SIZEOF_RSRC_ICON_GROUP_ENTRY; // entries
306 LPBYTE group = new BYTE[groupsize];
307 memset(group, 0, groupsize); // Reproducible builds (bug #1230)
308
309 IconGroupHeader* header = (IconGroupHeader*) group;
310
311 header->wReserved = 0;
312 header->wType = FIX_ENDIAN_INT16(ICO_TYPE_ICON);
313 header->wCount = FIX_ENDIAN_INT16((WORD)icon.size());
314
315 order = sort_pairs(order, first);
316
317 for (IconGroup::size_type i = 0; i < icon.size(); i++)
318 {
319 RsrcIconGroupEntry* entry = (RsrcIconGroupEntry*)
320 &group[sizeof(IconGroupHeader) + SIZEOF_RSRC_ICON_GROUP_ENTRY * i];
321 unsigned index = first ? order[i].index1 : order[i].index2;
322
323 memcpy(&entry->header, &icon[index].meta, sizeof(IconGroupEntry));
324 entry->wRsrcId = FIX_ENDIAN_INT16(order[i].size_index + 1);
325 }
326
327 return group;
328 }
329
330 // set_icon, must get an initialized resource editor
331 void set_main_icon(CResourceEditor* re, WORD wIconId, IconGroup icon1, IconGroup icon2)
332 {
333 IconPairs order = get_icon_order(icon1, icon2);
334
335 // generate group
336 LPBYTE group1 = generate_icon_group(icon1, order, true);
337
338 // set group
339 size_t group_size = sizeof(IconGroupHeader) // header
340 + order.size() * SIZEOF_RSRC_ICON_GROUP_ENTRY; // entries
341
342 re->UpdateResource(RT_GROUP_ICON, wIconId, NSIS_DEFAULT_LANG, group1, (DWORD)group_size, CResourceEditor::TM_RAW); // Update the group entries but not the images
343 destroy_icon_group(group1);
344
345 // delete old icons
346 unsigned i = 1;
347 while (i <= MAIN_ICON_LAST_IMAGE && re->UpdateResource(RT_ICON, i++, NSIS_DEFAULT_LANG, 0, 0));
348
349 // set new icons
350 IconGroup::size_type order_index;
351 for (order_index = 0; order_index < order.size(); order_index++)
352 {
353 WORD size_index = order[order_index].size_index;
354 DWORD size = order[order_index].size;
355 LPBYTE data = new BYTE[size];
356 memset(data, 0, size);
357
358 if (order_index < icon1.size())
359 {
360 Icon* icon = &icon1[order[order_index].index1];
361 memcpy(data, icon->data, FIX_ENDIAN_INT32(icon->meta.dwRawSize));
362 }
363
364 re->UpdateResource(RT_ICON, size_index + 1, NSIS_DEFAULT_LANG, data, size);
365
366 delete [] data;
367 }
368 }
369
370 #ifdef NSIS_CONFIG_UNINSTALL_SUPPORT
371 // returns the data of the uninstaller icon that should replace the installer icon data
372 unsigned char* generate_uninstall_icon_data(IconGroup icon1, IconGroup icon2, size_t &data_size)
373 {
374 IconGroup::size_type i;
375 IconPairs order = get_icon_order(icon1, icon2);
376
377 // generate group
378 LPBYTE group = generate_icon_group(icon2, order, false);
379
380 // calculate size
381 size_t group_size = sizeof(IconGroupHeader) // header
382 + order.size() * SIZEOF_RSRC_ICON_GROUP_ENTRY; // entries
383
384 data_size = group_size // group header
385 + sizeof(DWORD) * 2 // offset and size of group header
386 + (sizeof(DWORD) * 2) * icon2.size() // offset and size per entry
387 + sizeof(DWORD); // terminator
388
389 for (i = 0; i < icon2.size(); i++)
390 {
391 // add icon sizes
392 data_size += FIX_ENDIAN_INT32(icon2[i].meta.dwRawSize);
393 }
394
395 // allocate memory
396 LPBYTE uninst_data = new BYTE[data_size];
397 LPBYTE seeker = uninst_data;
398
399 // fill group header
400 *(LPDWORD) seeker = FIX_ENDIAN_INT32((UINT32)group_size);
401 seeker += sizeof(DWORD);
402 *(LPDWORD) seeker = 0;
403 seeker += sizeof(DWORD);
404
405 memcpy(seeker, group, group_size);
406 seeker += group_size;
407
408 // fill entries
409 for (i = 0; i < icon2.size(); i++)
410 {
411 Icon* icon = &icon2[order[i].index2];
412 DWORD size = icon->meta.dwRawSize;
413
414 memcpy(seeker, &size, sizeof(size));
415 seeker += sizeof(size);
416 memset(seeker, 0, sizeof(DWORD));
417 seeker += sizeof(DWORD);
418 memcpy(seeker, icon->data, size);
419 seeker += FIX_ENDIAN_INT32(size);
420 }
421
422 // add terminator
423 memset(seeker, 0, sizeof(DWORD));
424
425 // done
426 destroy_icon_group(group);
427 return uninst_data;
428 }
429
430 // Fill the array of icons for uninstall with their offsets
431 // Returns zero on failure
432 int generate_unicons_offsets(LPBYTE exeHeader, size_t exeHeaderSize, LPBYTE uninstIconData, WORD wIconId)
433 {
434 try
435 {
436 DWORD offset;
437 DWORD size;
438
439 CResourceEditor re(exeHeader, (DWORD)exeHeaderSize, false);
440
441 LPBYTE seeker = uninstIconData;
442
443 offset = re.GetResourceOffset(RT_GROUP_ICON, wIconId, NSIS_DEFAULT_LANG);
444
445 size = FIX_ENDIAN_INT32(*(LPDWORD)seeker);
446 seeker += sizeof(DWORD);
447 *(LPDWORD) seeker = FIX_ENDIAN_INT32(offset);
448 seeker += sizeof(DWORD);
449
450 seeker += size;
451
452 WORD icon_index = 1;
453
454 while (*(LPDWORD)seeker)
455 {
456 offset = re.GetResourceOffset(RT_ICON, icon_index, NSIS_DEFAULT_LANG);
457
458 if (offset > exeHeaderSize)
459 {
460 throw runtime_error("invalid icon offset (possibly compressed icon)");
461 }
462
463 DWORD real_size = re.GetResourceSize(RT_ICON, icon_index, NSIS_DEFAULT_LANG);
464
465 size = FIX_ENDIAN_INT32(*(LPDWORD)seeker);
466 seeker += sizeof(DWORD);
467
468 if (real_size < size) // uninst icon could be smaller, in case we don't have perfect matches
469 {
470 throw runtime_error("invalid icon size (possibly compressed icon)");
471 }
472
473 *(LPDWORD) seeker = FIX_ENDIAN_INT32(offset);
474 seeker += sizeof(DWORD);
475
476 seeker += size;
477
478 icon_index++;
479 }
480 }
481 catch (const exception& e)
482 {
483 if (g_display_errors)
484 PrintColorFmtMsg_ERR(_T("\nError generating uninstaller icon: %") NPRIs _T(" -- failing!\n"), CtoTStrParam(e.what()));
485 return 0;
486 }
487
488 return 1;
489 }
490 #endif // NSIS_CONFIG_UNINSTALL_SUPPORT