|
| 1 | +# frozen_string_literal: true |
| 2 | +require "fiddle/import" |
| 3 | +require "fiddle/types" |
| 4 | + |
| 5 | +module Ocran |
| 6 | + # Changes the Icon in a PE executable. |
| 7 | + module EdIcon |
| 8 | + extend Fiddle::Importer |
| 9 | + dlload "kernel32.dll" |
| 10 | + |
| 11 | + include Fiddle::Win32Types |
| 12 | + typealias "LPVOID", "void*" |
| 13 | + typealias "LPCWSTR", "char*" |
| 14 | + |
| 15 | + module Successive |
| 16 | + include Enumerable |
| 17 | + |
| 18 | + def each |
| 19 | + return to_enum(__method__) unless block_given? |
| 20 | + |
| 21 | + entry = self |
| 22 | + while true |
| 23 | + yield(entry) |
| 24 | + entry = self.class.new(tail) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def tail |
| 29 | + to_ptr + self.class.size |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # Icon file header |
| 34 | + IconHeader = struct( |
| 35 | + [ |
| 36 | + "WORD Reserved", |
| 37 | + "WORD ResourceType", |
| 38 | + "WORD ImageCount" |
| 39 | + ] |
| 40 | + ).include(Successive) |
| 41 | + |
| 42 | + icon_info = [ |
| 43 | + "BYTE Width", |
| 44 | + "BYTE Height", |
| 45 | + "BYTE Colors", |
| 46 | + "BYTE Reserved", |
| 47 | + "WORD Planes", |
| 48 | + "WORD BitsPerPixel", |
| 49 | + "DWORD ImageSize" |
| 50 | + ] |
| 51 | + |
| 52 | + # Icon File directory entry structure |
| 53 | + IconDirectoryEntry = struct(icon_info + ["DWORD ImageOffset"]).include(Successive) |
| 54 | + |
| 55 | + # Group Icon Resource directory entry structure |
| 56 | + IconDirResEntry = struct(icon_info + ["WORD ResourceID"]).include(Successive) |
| 57 | + |
| 58 | + class IconFile < IconHeader |
| 59 | + def initialize(icon_filename) |
| 60 | + @data = File.binread(icon_filename) |
| 61 | + super(Fiddle::Pointer.to_ptr(@data)) |
| 62 | + end |
| 63 | + |
| 64 | + def entries |
| 65 | + IconDirectoryEntry.new(self.tail).take(self.ImageCount) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + class GroupIcon < IconHeader |
| 70 | + attr_reader :size |
| 71 | + |
| 72 | + def initialize(image_count, resource_type) |
| 73 | + @size = IconHeader.size + image_count * IconDirResEntry.size |
| 74 | + super(Fiddle.malloc(@size), Fiddle::RUBY_FREE) |
| 75 | + self.Reserved = 0 |
| 76 | + self.ResourceType = resource_type |
| 77 | + self.ImageCount = image_count |
| 78 | + end |
| 79 | + |
| 80 | + def entries |
| 81 | + IconDirResEntry.new(self.tail).take(self.ImageCount) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + INVALID_HANDLE_VALUE = Fiddle::Pointer.new(-1) |
| 86 | + |
| 87 | + MAKEINTRESOURCE = -> (i) { Fiddle::Pointer.new(i) } |
| 88 | + RT_ICON = MAKEINTRESOURCE.(3) |
| 89 | + RT_GROUP_ICON = MAKEINTRESOURCE.(RT_ICON.to_i + 11) |
| 90 | + |
| 91 | + MAKELANGID = -> (p, s) { s << 10 | p } |
| 92 | + LANG_NEUTRAL = 0x00 |
| 93 | + SUBLANG_DEFAULT = 0x01 |
| 94 | + LANGID = MAKELANGID.(LANG_NEUTRAL, SUBLANG_DEFAULT) |
| 95 | + |
| 96 | + extern "DWORD GetLastError()" |
| 97 | + extern "HANDLE BeginUpdateResourceW(LPCWSTR, BOOL)" |
| 98 | + extern "BOOL EndUpdateResourceW(HANDLE, BOOL)" |
| 99 | + extern "BOOL UpdateResourceW(HANDLE, LPCWSTR, LPCWSTR, WORD, LPVOID, DWORD)" |
| 100 | + |
| 101 | + class << self |
| 102 | + def update_icon(executable_filename, icon_filename) |
| 103 | + update_resource(executable_filename) do |handle| |
| 104 | + icon_file = IconFile.new(icon_filename) |
| 105 | + icon_entries = icon_file.entries |
| 106 | + |
| 107 | + # Create the RT_ICON resources |
| 108 | + icon_entries.each_with_index do |entry, i| |
| 109 | + if UpdateResourceW(handle, RT_ICON, 101 + i, LANGID, icon_file.to_i + entry.ImageOffset, entry.ImageSize) == 0 |
| 110 | + raise "failed to UpdateResource(#{GetLastError()})" |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + # Create the RT_GROUP_ICON structure |
| 115 | + group_icon = GroupIcon.new(icon_file.ImageCount, icon_file.ResourceType) |
| 116 | + group_icon.entries.zip(icon_entries).each_with_index do |(res, icon), i| |
| 117 | + res.Width = icon.Width |
| 118 | + res.Height = icon.Height |
| 119 | + res.Colors = icon.Colors |
| 120 | + res.Reserved = icon.Reserved |
| 121 | + res.Planes = icon.Planes |
| 122 | + res.BitsPerPixel = icon.BitsPerPixel |
| 123 | + res.ImageSize = icon.ImageSize |
| 124 | + res.ResourceID = 101 + i |
| 125 | + end |
| 126 | + |
| 127 | + # Save the RT_GROUP_ICON resource |
| 128 | + if UpdateResourceW(handle, RT_GROUP_ICON, 100, LANGID, group_icon, group_icon.size) == 0 |
| 129 | + raise "Failed to create group icon(#{GetLastError()})" |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + def update_resource(executable_filename) |
| 135 | + handle = BeginUpdateResourceW(executable_filename.encode("UTF-16LE"), 0) |
| 136 | + if handle == Fiddle::NULL |
| 137 | + raise "Failed to BeginUpdateResourceW(#{GetLastError()})" |
| 138 | + end |
| 139 | + |
| 140 | + yield(handle) |
| 141 | + |
| 142 | + if EndUpdateResourceW(handle, 0) == 0 |
| 143 | + raise "Failed to EndUpdateResourceW(#{GetLastError()})" |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | +end |
0 commit comments