1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
/*
* peicon - extract icons from Portable Executable (PE) files
* Copyright by Jan Engelhardt, 2013
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* For details, see the file named "LICENSE.GPL2".
*
* See http://msdn.microsoft.com/en-us/library/ms997538.aspx for a
* description of the ICO and PE formats.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <libHX/defs.h>
#include <libHX/map.h>
#include <libHX/option.h>
struct mzhdr {
uint8_t magic[2];
uint16_t unused[13];
uint8_t reserved[32];
uint32_t pe_offset;
};
struct pecoffhdr {
uint8_t magic[4];
uint16_t machine;
uint16_t num_sections;
uint32_t xtime;
uint32_t symtab_ptr;
uint32_t num_symbols;
uint16_t opthdr_size;
uint16_t flags;
};
struct pesecthdr {
char name[8];
uint32_t virt_size, virt_addr;
uint32_t size, offset;
uint32_t reloctbl_ofs, lntbl_ofs;
uint16_t reloc_count, ln_count;
uint32_t flags;
};
struct resdir {
uint32_t unused[2];
uint16_t unused2[2], name_count, id_count;
};
struct resdirent {
uint32_t name, offset;
};
struct resdataent {
uint32_t offset, size, codepage, reserved;
};
struct icondir {
uint16_t reserved, type, count;
} __attribute__((packed));
#define grpicondir icondir
struct icondirentry {
uint8_t width, height, colors, reserved;
uint16_t planes, bpp;
uint32_t bytes, image_offset;
} __attribute__((packed));
struct grpicondirentry {
uint8_t width, height, colors, reserved;
uint16_t planes, bpp;
uint32_t bytes;
uint16_t id;
} __attribute__((packed));
/**
* @re_inum: offset of this RE from the file
* @data_inum: offset of the data from the file
* @fs_name: display name
*/
struct Z_resdirent {
const char *base;
unsigned int re_from_base, re_from_section;
unsigned int data_from_base, data_from_section;
unsigned int re_name;
char fs_name[64], fs_type;
};
struct Z_resdir {
const char *base;
unsigned int section_from_base, rd_from_section, re_from_section;
unsigned int end_from_section;
struct Z_resdirent dentry;
};
struct mblock {
const char *data;
unsigned int size;
};
enum {
RT_ICON = 0x03,
RT_GROUP_ICON = 0x0e,
};
static int global_shift;
static unsigned int verbose, verbose_dirlist, verbose_filelist;
static struct HXmap *icon_list;
static const struct HXoption options_table[] = {
{.sh = 'L', .type = HXTYPE_NONE, .ptr = &verbose_dirlist},
{.sh = 'l', .type = HXTYPE_NONE, .ptr = &verbose_filelist},
{.sh = 'o', .type = HXTYPE_INT, .ptr = &global_shift},
{.sh = 'v', .type = HXTYPE_NONE | HXOPT_INC, .ptr = &verbose},
HXOPT_AUTOHELP,
HXOPT_TABLEEND,
};
static bool verb_show(const struct Z_resdirent *rde)
{
if (rde->fs_type == 'd' && verbose_dirlist)
return true;
if (rde->fs_type == 'f' && verbose_filelist)
return true;
return false;
}
static bool re_isdir(const struct resdirent *re)
{
return re->offset & (1 << 31);
}
static unsigned int re_offset(const struct resdirent *re)
{
return re->offset & ~(1 << 31);
}
static struct Z_resdir *
resdir_open(const char *base, unsigned int sfb, unsigned int dfs)
{
const struct resdir *rd;
struct Z_resdir *dh;
dh = malloc(sizeof(*dh));
if (dh == NULL)
return NULL;
rd = reinterpret_cast(const struct resdir *, base + sfb + dfs);
dh->base = base;
dh->section_from_base = sfb;
dh->rd_from_section = dfs;
dh->re_from_section = 0;
dh->end_from_section = dfs + sizeof(*rd) + sizeof(struct resdirent) *
(rd->name_count + rd->id_count);
return dh;
}
static const struct Z_resdirent *resdir_walk(struct Z_resdir *dh)
{
const struct resdirent *re;
struct Z_resdirent *de = &dh->dentry;
memset(de, 0, sizeof(*de));
de->base = dh->base;
if (dh->re_from_section == 0) {
/* dir itself */
de->fs_type = 'd';
de->re_from_section = dh->rd_from_section;
de->re_from_base = dh->section_from_base + de->re_from_section;
de->data_from_section = de->re_from_section + sizeof(struct resdir);
de->data_from_base = dh->section_from_base + de->data_from_section;
snprintf(de->fs_name, sizeof(de->fs_name), ".");
dh->re_from_section = dh->rd_from_section + sizeof(struct resdir);
return de;
} else if (dh->re_from_section >= dh->end_from_section) {
return NULL;
}
re = reinterpret_cast(const struct resdirent *,
dh->base + dh->section_from_base + dh->re_from_section);
de->fs_type = re_isdir(re) ? 'd' : 'f';
de->re_name = re->name;
de->re_from_section = dh->re_from_section;
de->re_from_base = dh->section_from_base + de->re_from_section;
de->data_from_section = re_offset(re);
de->data_from_base = dh->section_from_base + de->data_from_section;
snprintf(de->fs_name, sizeof(de->fs_name), "%x@%x",
re->name, de->data_from_base);
dh->re_from_section += sizeof(*re);
return de;
}
static void resdir_close(struct Z_resdir *dh)
{
free(dh);
}
static int record_icon(uintptr_t icon_id, const struct mblock *mblk)
{
const void *icon_key = reinterpret_cast(void *, icon_id);
int ret;
ret = HXmap_add(icon_list, icon_key, mblk);
if (ret <= 0) {
fprintf(stderr, "HXmap_add: %s\n", strerror(-ret));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static const struct grpicondirentry *
grpicondirentry_next(const struct grpicondirentry *e)
{
return reinterpret_cast(const struct grpicondirentry *,
reinterpret_cast(const char *, e) + sizeof(*e));
}
static void print_iconsubdir_entry(const struct Z_resdirent *icon_de,
const struct Z_resdirent *de, const struct resdataent *rm)
{
printf("%x %c", de->re_from_base, de->fs_type);
if (de->fs_type == 'd')
printf(" %5u /icons/%s/%s (unhandled)",
0, icon_de->fs_name, de->fs_name);
else
printf(" %5u /icons/%s/%s:%x",
rm->size, icon_de->fs_name, de->fs_name,
rm->offset - global_shift);
printf("\n");
}
static int parse_iconsubdir(const struct Z_resdirent *icon_de,
const char *base, unsigned int section_from_base)
{
struct Z_resdir *dh;
const struct Z_resdirent *de;
const struct resdataent *rm;
struct mblock mblk;
int ret = EXIT_SUCCESS;
dh = resdir_open(base, section_from_base, icon_de->data_from_section);
if (dh == NULL) {
perror("resdir_open");
return EXIT_FAILURE;
}
while ((de = resdir_walk(dh)) != NULL) {
if (strcmp(de->fs_name, ".") == 0)
continue;
rm = reinterpret_cast(const struct resdataent *,
de->base + de->data_from_base);
if (verb_show(de))
print_iconsubdir_entry(icon_de, de, rm);
if (de->fs_type == 'd')
continue;
mblk.data = de->base + rm->offset - global_shift;
mblk.size = rm->size;
ret = record_icon(icon_de->re_name, &mblk);
if (ret != EXIT_SUCCESS)
break;
}
resdir_close(dh);
return ret;
}
static void print_icondir_entry(const struct Z_resdirent *de)
{
printf("%x %c", de->re_from_base, de->fs_type);
printf(" %5u /icons/%s", 0, de->fs_name);
if (de->fs_type != 'd')
printf(" (unhandled)");
printf("\n");
}
static int parse_icondir(const char *base, unsigned int section_from_base,
unsigned int rd_from_section)
{
const struct Z_resdirent *de;
struct Z_resdir *dh;
int ret = EXIT_SUCCESS;
dh = resdir_open(base, section_from_base, rd_from_section);
if (dh == NULL) {
perror("resdir_open");
return EXIT_FAILURE;
}
while ((de = resdir_walk(dh)) != NULL) {
if (strcmp(de->fs_name, ".") == 0)
continue;
if (verb_show(de))
print_icondir_entry(de);
if (de->fs_type != 'd')
continue;
ret = parse_iconsubdir(de, dh->base, dh->section_from_base);
if (ret != EXIT_SUCCESS)
break;
}
resdir_close(dh);
return ret;
}
static void print_giconent(const struct Z_resdirent *gicon_de,
const struct Z_resdirent *grs_de, const struct mblock *mblk,
unsigned int i, const struct grpicondirentry *ge)
{
printf("0 l /group_icon/%s/%s:%lx/%x (%ux%ux%u) -> /icons/%x\n",
gicon_de->fs_name, grs_de->fs_name,
static_cast(long, mblk->data - gicon_de->base), i,
ge->width, ge->height, ge->bpp, ge->id);
}
static void gide_to_ide(struct icondirentry *i,
const struct grpicondirentry *g)
{
i->width = g->width;
i->height = g->height;
i->colors = g->colors;
i->reserved = g->reserved;
i->planes = g->planes;
i->bpp = g->bpp;
i->bytes = g->bytes;
}
static int parse_giconent(const struct Z_resdirent *gicon_de,
const struct Z_resdirent *grs_de, const struct mblock *mblk)
{
const struct mblock *icon_mblk;
const struct grpicondir *gd;
const struct grpicondirentry *ge;
struct icondir id;
struct icondirentry ie;
unsigned int i;
char filename[64];
int fd;
gd = reinterpret_cast(const struct icondir *, mblk->data);
if (gd->type != 1)
return EXIT_SUCCESS;
id = *gd;
id.count = 1;
ge = reinterpret_cast(const struct grpicondirentry *,
mblk->data + sizeof(*gd));
for (i = 0; i < gd->count; ++i, ge = grpicondirentry_next(ge)) {
if (verbose_filelist)
print_giconent(gicon_de, grs_de, mblk, i, ge);
icon_mblk = HXmap_get(icon_list,
reinterpret_cast(const void *,
static_cast(uintptr_t, ge->id)));
if (icon_mblk == NULL) {
fprintf(stderr, "No such icon %u\n", ge->id);
return EXIT_FAILURE;
}
snprintf(filename, sizeof(filename), "pe-%x:%x.ico",
gicon_de->re_name, i);
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUGO | S_IWUGO);
gide_to_ide(&ie, ge);
ie.image_offset = sizeof(id) + sizeof(ie);
printf("x %s (%u bytes, %ux%ux%u)\n", filename,
ie.image_offset + icon_mblk->size,
ie.width, ie.height, ie.bpp);
if (fd < 0) {
fprintf(stderr, "Could not open %s for writing: %s\n",
filename, strerror(errno));
return EXIT_FAILURE;
}
write(fd, &id, sizeof(id));
write(fd, &ie, sizeof(ie));
write(fd, icon_mblk->data, icon_mblk->size);
close(fd);
}
return EXIT_SUCCESS;
}
static void print_giconsubdir_entry(const struct Z_resdirent *gicon_de,
const struct Z_resdirent *de, const struct resdataent *rm)
{
printf("%x %c", de->re_from_base, de->fs_type);
if (de->fs_type == 'd')
printf(" %5u /group_icons/%s/%s (unhandled)",
0, gicon_de->fs_name, de->fs_name);
else
printf(" %5u /group_icons/%s/%s:%x",
rm->size, gicon_de->fs_name, de->fs_name,
rm->offset - global_shift);
printf("\n");
}
static int parse_giconsubdir(const struct Z_resdirent *gicon_de,
const char *base, unsigned int section_from_base)
{
struct Z_resdir *dh;
const struct Z_resdirent *de;
const struct resdataent *rm;
int ret = EXIT_SUCCESS;
struct mblock mblk;
dh = resdir_open(base, section_from_base, gicon_de->data_from_section);
if (dh == NULL) {
perror("resdir_open");
return EXIT_FAILURE;
}
while ((de = resdir_walk(dh)) != NULL) {
if (strcmp(de->fs_name, ".") == 0)
continue;
rm = reinterpret_cast(const struct resdataent *,
de->base + de->data_from_base);
if (verb_show(de))
print_giconsubdir_entry(gicon_de, de, rm);
if (de->fs_type == 'd')
continue;
mblk.data = de->base + rm->offset - global_shift;
mblk.size = rm->size;
ret = parse_giconent(gicon_de, de, &mblk);
if (ret != EXIT_SUCCESS)
break;
}
resdir_close(dh);
return ret;
}
static int parse_gicondir(const char *base, unsigned int section_from_base,
unsigned int rd_from_section)
{
const struct Z_resdirent *de;
struct Z_resdir *dh;
int ret = EXIT_SUCCESS;
dh = resdir_open(base, section_from_base, rd_from_section);
if (dh == NULL) {
perror("resdir_open");
return EXIT_FAILURE;
}
while ((de = resdir_walk(dh)) != NULL) {
if (strcmp(de->fs_name, ".") == 0)
continue;
if (verb_show(de)) {
printf("%x %c", de->re_from_base, de->fs_type);
printf(" %5u /group_icons/%s", 0, de->fs_name);
if (de->fs_type != 'd')
printf(" (unhandled)");
printf("\n");
}
if (de->fs_type != 'd')
continue;
ret = parse_giconsubdir(de, dh->base, dh->section_from_base);
if (ret != EXIT_SUCCESS)
break;
}
resdir_close(dh);
return ret;
}
static int parse_resdir(const char *base, unsigned int section_from_base,
unsigned int rd_from_section)
{
const struct Z_resdirent *de;
struct Z_resdir *dh;
int ret = EXIT_SUCCESS;
dh = resdir_open(base, section_from_base, rd_from_section);
if (dh == NULL) {
perror("resdir_open");
return EXIT_FAILURE;
}
while ((de = resdir_walk(dh)) != NULL) {
if (verb_show(de)) {
printf("%x %c", de->re_from_base, de->fs_type);
printf(" %5u /%s", 0, de->fs_name);
if (de->re_name == RT_ICON)
printf(" => icons");
else if (de->re_name == RT_GROUP_ICON)
printf(" => group_icons");
printf("\n");
}
ret = EXIT_SUCCESS;
if (de->re_name == RT_ICON)
ret = parse_icondir(dh->base, dh->section_from_base,
de->data_from_section);
else if (de->re_name == RT_GROUP_ICON)
ret = parse_gicondir(dh->base, dh->section_from_base,
de->data_from_section);
if (ret != EXIT_SUCCESS)
break;
}
resdir_close(dh);
return ret;
}
static int parse_pecoffhdr(const char *base, unsigned int pe_offset)
{
const struct pecoffhdr *pe;
const struct pesecthdr *sh;
unsigned int i;
int ret;
unsigned int sh_offset;
pe = reinterpret_cast(const struct pecoffhdr *, base + pe_offset);
if (pe->magic[0] != 'P' || pe->magic[1] != 'E' ||
pe->magic[2] != 0x00 || pe->magic[3] != 0x00) {
printf("ERROR: Not a PE file\n");
return EXIT_FAILURE;
}
if (verbose >= 2) {
printf("PE magic checked out.\n");
printf("PE: Number of sections: %u\n", pe->num_sections);
printf("PE: Optional header size 0x%x\n", pe->opthdr_size);
}
sh_offset = pe_offset + sizeof(*pe) + pe->opthdr_size;
for (i = 0; i < pe->num_sections; ++i) {
sh = reinterpret_cast(const struct pesecthdr *, base + sh_offset);
if (verbose >= 2)
printf("PE: Section %u (\"%.8s\") at 0x%x\n",
i, sh->name, sh_offset);
if (strncmp(sh->name, ".rsrc", sizeof(sh->name)) == 0) {
global_shift = (intptr_t)sh->virt_addr - (intptr_t)sh->offset;
if (verbose >= 2) {
printf("SH: Resource section header\n");
printf("SH: Start 0x%x size 0x%x\n", sh->offset, sh->size);
printf("SH: VA->FA shift = %d\n\n", global_shift);
}
ret = parse_resdir(base, sh->offset, 0);
if (ret != EXIT_SUCCESS)
return ret;
}
sh_offset += sizeof(*sh);
}
return EXIT_SUCCESS;
}
static int parse_mzhdr(const void *base)
{
const struct mzhdr *mz = base;
if (mz->magic[0] != 'M' || mz->magic[1] != 'Z') {
printf("ERROR: Not an MZ file\n");
return EXIT_FAILURE;
}
if (verbose >= 2) {
printf("MZ magic checked out.\n");
printf("MZ: PE header at 0x%x\n", mz->pe_offset);
}
return parse_pecoffhdr(base, mz->pe_offset);
}
int main(int argc, const char **argv)
{
struct stat sb;
void *base;
int fd, ret;
ret = HX_getopt(options_table, &argc, &argv, HXOPT_USAGEONERR);
if (ret != HXOPT_ERR_SUCCESS)
return EXIT_FAILURE;
icon_list = HXmap_init5(HXMAPT_DEFAULT, HXMAP_CDATA,
NULL, 0, sizeof(struct mblock));
if (icon_list == NULL) {
perror("HXmap_init");
return EXIT_FAILURE;
}
if (argc < 2) {
fprintf(stderr, "Extract what file?\n");
return EXIT_FAILURE;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Could not open %s for reading: %s\n",
argv[1], strerror(errno));
return EXIT_FAILURE;
}
if (fstat(fd, &sb) < 0) {
perror("fstat");
return EXIT_FAILURE;
}
base = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (base == reinterpret_cast(void *, -1UL)) {
perror("mmap");
return EXIT_FAILURE;
}
close(fd);
ret = parse_mzhdr(base);
munmap(base, sb.st_size);
printf("Found %u icon bitmaps\n", icon_list->items);
HXmap_free(icon_list);
return ret;
}
|