File tree Expand file tree Collapse file tree 5 files changed +44
-19
lines changed Expand file tree Collapse file tree 5 files changed +44
-19
lines changed Original file line number Diff line number Diff line change @@ -263,17 +263,26 @@ defmodule LibPE do
263
263
LibPE.ResourceTable . parse ( virtual_data , virtual_address )
264
264
265
265
nil ->
266
- nil
266
+ % LibPE.ResourceTable { }
267
267
end
268
268
end
269
269
270
- def set_resources ( % LibPE { coff_sections: sections } = pe , resources = % LibPE.ResourceTable { } ) do
270
+ defp ensure_resource_section ( % LibPE { coff_sections: sections } = pe ) do
271
+ idx = Enum . find_index ( sections , fn % LibPE.Section { name: name } -> name == ".rsrc" end )
272
+
273
+ if idx == nil do
274
+ % LibPE { pe | coff_sections: sections ++ [ % LibPE.Section { name: ".rsrc" } ] }
275
+ else
276
+ pe
277
+ end
278
+ end
279
+
280
+ def set_resources ( pe , resources = % LibPE.ResourceTable { } ) do
271
281
# need to ensure that the virtual_address is up-to-date
272
- pe = update_layout ( pe )
282
+ % LibPE { coff_sections: sections } = pe = update_layout ( ensure_resource_section ( pe ) )
273
283
274
284
# now fetching and setting the resource
275
285
idx = Enum . find_index ( sections , fn % LibPE.Section { name: name } -> name == ".rsrc" end )
276
-
277
286
section = % LibPE.Section { virtual_address: virtual_address } = Enum . at ( sections , idx )
278
287
data = LibPE.ResourceTable . encode ( resources , virtual_address )
279
288
section = % LibPE.Section { section | virtual_data: data }
Original file line number Diff line number Diff line change @@ -2,21 +2,19 @@ defmodule LibPE.Section do
2
2
@ moduledoc false
3
3
alias LibPE.Section
4
4
5
- defstruct [
6
- :name ,
7
- :padding ,
8
- :virtual_data ,
9
- :virtual_size ,
10
- :virtual_address ,
11
- :raw_data ,
12
- :size_of_raw_data ,
13
- :pointer_to_raw_data ,
14
- :pointer_to_relocations ,
15
- :pointer_to_linenumbers ,
16
- :number_of_relocations ,
17
- :number_of_linenumbers ,
18
- :flags
19
- ]
5
+ defstruct name: nil ,
6
+ padding: "\0 " ,
7
+ virtual_data: "" ,
8
+ virtual_size: 0 ,
9
+ virtual_address: 0 ,
10
+ raw_data: "" ,
11
+ size_of_raw_data: 0 ,
12
+ pointer_to_raw_data: 0 ,
13
+ pointer_to_relocations: 0 ,
14
+ pointer_to_linenumbers: 0 ,
15
+ number_of_relocations: 0 ,
16
+ number_of_linenumbers: 0 ,
17
+ flags: 0
20
18
21
19
def parse ( rest , number , full_image ) do
22
20
List . duplicate ( nil , number )
Original file line number Diff line number Diff line change @@ -44,6 +44,24 @@ defmodule LibPETest do
44
44
end
45
45
end
46
46
47
+ test "set icon" do
48
+ { :ok , pe } = LibPE . parse_file ( "test/hello.exe" )
49
+
50
+ resource_table = LibPE . get_resources ( pe )
51
+
52
+ data = File . read! ( "test/logo.ico" )
53
+ type = LibPE.ResourceTypes . encode ( "RT_ICON" )
54
+ resource_table = LibPE.ResourceTable . set_resource ( resource_table , type , data )
55
+
56
+ raw =
57
+ LibPE . set_resources ( pe , resource_table )
58
+ |> LibPE . update_layout ( )
59
+ |> LibPE . update_checksum ( )
60
+ |> LibPE . encode ( )
61
+
62
+ File . write! ( "test/hello-out.exe" , raw )
63
+ end
64
+
47
65
# defp tip(rsrc) do
48
66
# clean_data(hd(rsrc.entries))
49
67
# end
You can’t perform that action at this time.
0 commit comments