Debian Codegraph @

terps/tads/tads2/msdos/seticon.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/seticon.c,v 1.2 1999/05/17 02:52:19 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 seticon.c - change an icon in an executable to one in an .ICO file
15 Function
16 Replaces an icon in a .EXE file with the icon in a .ICO file. This
17 function cannot add resources to an exectuable, but can only overwrite
18 existing resources; hence, we can only replace an existing icon in the
19 .EXE file with an icon with exactly the same characteristics. If the
20 .ICO file contains multiple types of icons (for example, a 16x16 and
21 a 32x32), we'll replace only the types that are already stored in the
22 .EXE file.
23 Notes
24
25 Modified
26 05/29/98 MJRoberts - Creation
27 */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <windows.h>
32
33 /* ------------------------------------------------------------------------ */
34 /*
35 * Icon resource file format structures
36 */
37
38 #pragma pack( push )
39 #pragma pack( 2 )
40
41 /* icon directory entry in ICO file */
42 typedef struct
43 {
44 BYTE bWidth; // Width, in pixels, of the image
45 BYTE bHeight; // Height, in pixels, of the image
46 BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
47 BYTE bReserved; // Reserved ( must be 0)
48 WORD wPlanes; // Color Planes
49 WORD wBitCount; // Bits per pixel
50 DWORD dwBytesInRes; // How many bytes in this resource?
51 DWORD dwImageOffset; // Where in the file is this image?
52 } ICONDIRENTRY, *LPICONDIRENTRY;
53
54 /* icon directory in ICO file */
55 typedef struct
56 {
57 WORD idReserved; // Reserved (must be 0)
58 WORD idType; // Resource Type (1 for icons)
59 WORD idCount; // How many images?
60 ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
61 } ICONDIR, *LPICONDIR;
62
63 /* group icon directory entry in EXE file */
64 typedef struct{
65 BYTE bWidth; // Width, in pixels, of the image
66 BYTE bHeight; // Height, in pixels, of the image
67 BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
68 BYTE bReserved; // Reserved
69 WORD wPlanes; // Color Planes
70 WORD wBitCount; // Bits per pixel
71 DWORD dwBytesInRes; // how many bytes in this resource?
72 WORD nID; // the ID
73 } GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
74
75 /* group icon directory header in EXE file */
76 typedef struct {
77 WORD idReserved; // Reserved (must be 0)
78 WORD idType; // Resource type (1 for icons)
79 WORD idCount; // How many images?
80 GRPICONDIRENTRY idEntries[1]; // The entries for each image
81 } GRPICONDIR, *LPGRPICONDIR;
82
83 #pragma pack( pop )
84
85
86 /* ------------------------------------------------------------------------ */
87 /*
88 * exit with an error
89 */
90 static void errexit(const char *msg)
91 {
92 printf("%s\n", msg);
93 exit(1);
94 }
95
96 /*
97 * Resource directory information structure
98 */
99 typedef struct res_info_t res_info_t;
100 struct res_info_t
101 {
102 /* seek address of the resource directory */
103 DWORD dir_pos;
104
105 /* virtual address base of resource data */
106 DWORD base_addr;
107 };
108
109 /*
110 * Look up the resource directory information. Fills in the res_info
111 * structure with the resource data. Returns zero on success, or a
112 * non-zero error code on failure.
113 */
114 int find_resource_dir(FILE *fp, res_info_t *res_info)
115 {
116 IMAGE_DOS_HEADER doshdr;
117 IMAGE_NT_HEADERS nthdr;
118 DWORD cnt;
119 DWORD lastsect;
120 DWORD start;
121
122 /* read the old-style DOS header */
123 if (fread(&doshdr, sizeof(doshdr), 1, fp) != 1)
124 return 1;
125 if (doshdr.e_magic != IMAGE_DOS_SIGNATURE)
126 return 2;
127
128 /* seek to the PE header and read that */
129 fseek(fp, doshdr.e_lfanew, SEEK_SET);
130 if (fread(&nthdr, sizeof(nthdr), 1, fp) != 1)
131 return 3;
132 if (nthdr.Signature != IMAGE_NT_SIGNATURE)
133 return 4;
134
135 /* find the first image section */
136 start = doshdr.e_lfanew
137 + FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader)
138 + nthdr.FileHeader.SizeOfOptionalHeader;
139 fseek(fp, start, SEEK_SET);
140
141 /* read image sections */
142 for (lastsect = 0, cnt = 0 ; cnt < nthdr.FileHeader.NumberOfSections ;
143 ++cnt)
144 {
145 IMAGE_SECTION_HEADER imghdr;
146 DWORD endp;
147
148 /* read this section header */
149 if (fread(&imghdr, sizeof(imghdr), 1, fp) != 1)
150 return 5;
151
152 /* if this is the resource section, return it */
153 if (memcmp(imghdr.Name, ".rsrc", IMAGE_SIZEOF_SHORT_NAME) == 0)
154 {
155 /* fill in the information */
156 res_info->dir_pos = imghdr.PointerToRawData;
157 res_info->base_addr = imghdr.VirtualAddress;
158
159 /* return success */
160 return 0;
161 }
162 }
163
164 /*
165 * we went through all the sections and failed to find a resource
166 * section - return an error
167 */
168 return 6;
169 }
170
171 /*
172 * Get a file offset and size for a resource, given the resource type
173 * and either the identifier or the index. If 'id_is_index' is true,
174 * the id is the 0-based index; otherwise, it's the ID. Returns zero if
175 * the resource is not found, otherwise returns the seek offset in the
176 * file of the resource's data, and fills in *data_size with the size of
177 * the data.
178 */
179 long find_resource(FILE *fp, long *data_size, const res_info_t *res_info,
180 LPSTR res_type, int res_id, int id_is_index)
181 {
182 IMAGE_RESOURCE_DIRECTORY dirhdr;
183 int i;
184 int found;
185 IMAGE_RESOURCE_DIRECTORY_ENTRY direntry;
186 IMAGE_RESOURCE_DATA_ENTRY dataentry;
187
188 /* read the root directory, which contains the type index */
189 fseek(fp, res_info->dir_pos, SEEK_SET);
190 if (fread(&dirhdr, sizeof(dirhdr), 1, fp) != 1)
191 return 0;
192
193 /* find the desired type */
194 for (i = 0, found = FALSE ;
195 i < dirhdr.NumberOfNamedEntries + dirhdr.NumberOfIdEntries ; ++i)
196 {
197 /* read the next entry */
198 if (fread(&direntry, sizeof(direntry), 1, fp) != 1)
199 return 0;
200
201 /* check to see if it's the type we want */
202 if (direntry.Id == (WORD)res_type)
203 {
204 found = TRUE;
205 break;
206 }
207 }
208
209 /* if we didn't find the type we want, give up */
210 if (!found)
211 return 0;
212
213 /* we found the type index - read its directory header */
214 fseek(fp, res_info->dir_pos + direntry.OffsetToDirectory, SEEK_SET);
215 if (fread(&dirhdr, sizeof(dirhdr), 1, fp) != 1)
216 return 0;
217
218 /* now search the directory for our index or ID */
219 for (i = 0, found = FALSE ;
220 i < dirhdr.NumberOfNamedEntries + dirhdr.NumberOfIdEntries ; ++i)
221 {
222 /* read the next entry */
223 if (fread(&direntry, sizeof(direntry), 1, fp) != 1)
224 return 0;
225
226 /* if this is the one we want, return it */
227 if ((id_is_index && res_id == i)
228 || (!id_is_index && !direntry.NameIsString
229 && direntry.Id == res_id))
230 {
231 found = TRUE;
232 break;
233 }
234 }
235
236 /* if we didn't find it, give up */
237 if (!found)
238 return 0;
239
240 /*
241 * got it - this entry will point to yet another directory, which
242 * will contain the actual data; seek to the directory and read its
243 * first member
244 */
245 fseek(fp, res_info->dir_pos + direntry.OffsetToDirectory, SEEK_SET);
246 if (fread(&dirhdr, sizeof(dirhdr), 1, fp) != 1
247 || fread(&direntry, sizeof(direntry), 1, fp) != 1)
248 return 0;
249
250 /* seek to and read the first directory entry's data header */
251 fseek(fp, res_info->dir_pos + direntry.OffsetToData, SEEK_SET);
252 if (fread(&dataentry, sizeof(dataentry), 1, fp) != 1)
253 return 0;
254
255 /* set the size from the directory entry */
256 *data_size = dataentry.Size;
257
258 /*
259 * compute and return the data address - note that the address is
260 * relative to the resource section's virtual address setting, so we
261 * must adjust the virtual address to determine the seek address in
262 * the file
263 */
264 return dataentry.OffsetToData - res_info->base_addr + res_info->dir_pos;
265 }
266
267 /*
268 * Replace the icon at icon_idx in the EXE file with the icon in the ICO
269 * file. Returns zero on success, non-zero on error.
270 */
271 int replace_icon(FILE *fpexe, int icon_idx, FILE *fpico)
272 {
273 res_info_t res_info;
274 long group_pos;
275 long group_size;
276 int err = 0;
277 int i;
278 GRPICONDIR group_dir;
279 GRPICONDIRENTRY *group_dir_entry = 0;
280 ICONDIR icon_dir;
281 ICONDIRENTRY *icon_dir_entry = 0;
282 ICONDIRENTRY *cur_ico;
283
284 /* read the ICO directory header */
285 if (fread(&icon_dir,
286 sizeof(icon_dir) - sizeof(ICONDIRENTRY), 1, fpico) != 1)
287 {
288 err = 101;
289 goto done;
290 }
291
292 /* read the ICO directory entries */
293 icon_dir_entry = (ICONDIRENTRY *)malloc(icon_dir.idCount
294 * sizeof(ICONDIRENTRY));
295 if (icon_dir_entry == 0)
296 {
297 err = 102;
298 goto done;
299 }
300 if (fread(icon_dir_entry, icon_dir.idCount * sizeof(ICONDIRENTRY),
301 1, fpico) != 1)
302 {
303 err = 103;
304 goto done;
305 }
306
307 /* find the resource directory in the EXE file */
308 err = find_resource_dir(fpexe, &res_info);
309 if (err != 0)
310 goto done;
311
312 /* look up the icon group rsource by index */
313 group_pos = find_resource(fpexe, &group_size, &res_info,
314 RT_GROUP_ICON, icon_idx, TRUE);
315
316 /* seek to and read the group icon directory */
317 fseek(fpexe, group_pos, SEEK_SET);
318 if (fread(&group_dir, sizeof(group_dir) - sizeof(GRPICONDIRENTRY),
319 1, fpexe) != 1)
320 {
321 err = 104;
322 goto done;
323 }
324
325 /* read the group directory entries */
326 group_dir_entry = (GRPICONDIRENTRY *)malloc(group_dir.idCount
327 * sizeof(GRPICONDIRENTRY));
328 if (group_dir_entry == 0)
329 {
330 err = 105;
331 goto done;
332 }
333 if (fread(group_dir_entry, group_dir.idCount * sizeof(GRPICONDIRENTRY),
334 1, fpexe) != 1)
335 {
336 err = 106;
337 goto done;
338 }
339
340 /*
341 * go through the image types in the replacement icon file; for each
342 * one, try to find the corresponding entry in the EXE file, and
343 * replace it
344 */
345 for (i = 0, cur_ico = icon_dir_entry ; i < icon_dir.idCount ;
346 ++i, ++cur_ico)
347 {
348 int j;
349 GRPICONDIRENTRY *cur_grp;
350 int found;
351
352 /* try to find this icon type in the EXE group icon directory */
353 for (found = FALSE, j = 0, cur_grp = group_dir_entry ;
354 j < group_dir.idCount ; ++j, ++cur_grp)
355 {
356 /* see if it matches */
357 if (cur_grp->bWidth == cur_ico->bWidth
358 && cur_grp->bHeight == cur_ico->bHeight
359 && cur_grp->bColorCount == cur_ico->bColorCount
360 && cur_grp->dwBytesInRes == cur_ico->dwBytesInRes)
361 {
362 long icon_pos;
363 long icon_size;
364 long rem;
365
366 /* note that we found it */
367 found = TRUE;
368
369 /* look up this icon entry */
370 icon_pos = find_resource(fpexe, &icon_size, &res_info,
371 RT_ICON, cur_grp->nID, FALSE);
372 if (icon_pos == 0)
373 {
374 err = 107;
375 goto done;
376 }
377
378 /* seek to the icon resource in the EXE file */
379 fseek(fpexe, icon_pos, SEEK_SET);
380
381 /* seek to the icon image in the ICO file */
382 fseek(fpico, cur_ico->dwImageOffset, SEEK_SET);
383
384 /* copy data from the ICO file to the EXE file */
385 for (rem = cur_ico->dwBytesInRes ; rem != 0 ; )
386 {
387 size_t cur;
388 char copybuf[512];
389
390 /*
391 * read up to the buffer size or the amount
392 * remaining, whichever is less
393 */
394 cur = sizeof(copybuf);
395 if (rem < (long)cur)
396 cur = (size_t)rem;
397 if (fread(copybuf, cur, 1, fpico) != 1)
398 {
399 err = 108;
400 goto done;
401 }
402
403 /* write it out to the EXE file */
404 if (fwrite(copybuf, cur, 1, fpexe) != 1)
405 {
406 err = 109;
407 goto done;
408 }
409
410 /* deduct the amount read from the total */
411 rem -= cur;
412 }
413
414 /* no need to look any further */
415 break;
416 }
417 }
418
419 /* if we didn't find it, report an error */
420 if (!found)
421 {
422 /*
423 * note the error, but keep looking anyway, because there
424 * may still be other resources that we can successfully
425 * use; this error is essentially just a warning that there
426 * were some icon types in the ICO file that we couldn't
427 * store into the EXE file
428 */
429 err = 110;
430 }
431 }
432
433 done:
434 /* free any memory we allocated */
435 if (group_dir_entry != 0)
436 free(group_dir_entry);
437 if (icon_dir_entry != 0)
438 free(icon_dir_entry);
439
440 /* return the result */
441 return err;
442 }
443
444 /* ------------------------------------------------------------------------ */
445 /*
446 * Test section
447 */
448
449 #if 0
450 int main(int argc, char **argv)
451 {
452 FILE *fpexe;
453 int icon_idx;
454 FILE *fpico;
455 int err;
456
457 /* check arguments */
458 if (argc != 4)
459 errexit("usage: seticon <exe-file> <icon-index> <icon-file>");
460
461 /* open the exe file */
462 if ((fpexe = fopen(argv[1], "r+b")) == 0)
463 errexit("error opening EXE file");
464
465 /* get the icon index in the EXE file */
466 icon_idx = atoi(argv[2]);
467
468 /* open the ICO file */
469 if ((fpico = fopen(argv[3], "rb")) == 0)
470 errexit("error opening ICO file");
471
472 /* go replace the icon */
473 err = replace_icon(fpexe, icon_idx, fpico);
474 if (err == 0)
475 printf("icon replaced!\n");
476 else
477 printf("error from replace_icon: %d\n", err);
478
479 /* done */
480 fclose(fpico);
481 fclose(fpexe);
482 exit(0);
483 return 0;
484 }
485
486 #endif