rcedit icon indicating copy to clipboard operation
rcedit copied to clipboard

When using rcedit on 7-Zip created archive, file gets corrupted

Open ghost opened this issue 6 years ago • 11 comments

I am trying to change the resources of an sfx exe archive created with 7-Zip. When I do so however, the content of the archive is replaced with the files for the resources. Yes I said replaced. If I do this with a large archive, the file size of the file is reduced dramatically. See below for details.

  1. With 7-Zip installed, create an sfx archive with the GUI or the following command. "C:\Program Files\7-Zip\7z.exe" a -sfx7z.sfx "<path of archive to create ending in .exe>" "<path of file or folder to archive>"
  2. Use the 7-Zip GUI to open the archive and view its contents which should be as expected. Then close the GUI.
  3. Run the following command. rcedit-x64.exe "<path of archive>" --set-version-string "LegalCopyright" "Copyright (c) 2017 <name>"
  4. Again use the 7-Zip GUI to open the archive. Where is the original file or folder that was archived?

ghost avatar Nov 25 '17 22:11 ghost

I'm also seeing issues with rcedit corrupting my executable after setting the version string. It could be this issue. Is it possible to get a fix?

thomasmfields avatar Feb 14 '18 13:02 thomasmfields

Same exact issue.

Mgamerz avatar Apr 14 '18 21:04 Mgamerz

yup. executables created with 7zsd.sfx will be corrupted randomly.

romjacket avatar Jul 02 '18 21:07 romjacket

I use 7sfx files from: https://github.com/chrislake/7zsfxmm and the following approach:

  1. rcedit.exe 7zsd.sfx <options>
  2. copy /b 7zsd.sfx + sfx_config.txt + MyPackage.7z MyPackage.exe

You'r changing the icon of the SFX module which is in front of the archive.

Draykee avatar May 23 '19 14:05 Draykee

You'r changing the icon of the SFX module which is in front of the archive.

yup. fixed. thanks.

romjacket avatar Sep 13 '19 21:09 romjacket

Oh, I get it! I was doing the commands Draykee posted in the opposite order and it was not working.

So maybe rcedit could be made to work even when the commands are completed the other way around so that other people would not have this problem?

ghost avatar Jun 16 '20 23:06 ghost

im having a similar issue with https://github.com/leafac/caxa when i build an exe then try using rcedit to say add an icon, its just Fatal error: Unable to commit changes then when you run the command again, it does apply the icon but the exe size has shrunk by 90% ???

si458 avatar Aug 07 '21 12:08 si458

Since rcedit relies on the BeginUpdateResource/UpdateResource/EndUpdateResource winapi functions to do all the actual work of modifying an exe, it appears those functions trim off anything past the "official" end of the .exe file (as defined by the file size in the header), which is a popular place to put extra program data, which will be ignored by the OS. So appears that the fix for this bug (and very likely also bug #91 and bug #51) would be to copy any such data out of the .exe and then paste it back on after calling EndUpdateResource.

rversteegen avatar Sep 05 '21 14:09 rversteegen

it appears those functions trim off anything past the "official" end of the .exe file

Indeed that's correct. dotnet5/6 binaries also store the application code (as IL) after the "official end" of the native binary, but it's worth noting that, at least in the dotnet case, simply copying the data away beforehand and putting it back at the end afterwards will not help - you'll still end up with a corrupt exe.

The "offset" pointing to this extra code stored at the end is written within the real executable code, so unless you know how to update this offset to match the new executable length, the exe will still be garbage.

I don't know for sure that other approaches will suffer the same problem (eg. 7z) but I suspect so, and it will likely be impossible to find a universal solution that works for every exe using this approach.

I work-around this problem in dotnet exe's specifically by extracting all of the embedded code at the end and then compiling a whole new binary bundle just like the SDK does it, setting the icon before re-appending the extracted code.

caesay avatar Mar 07 '22 20:03 caesay

Oh, yes, if there's an offset somewhere to the appended data then rcedit can't hope to support it. But that's why appending zip files to an exe is so popular, it works because the header is at the end of the file without needing to hardcode the offset. And so I expect that what I described will work in quite a lot of cases. Don't know about 7zip.

rversteegen avatar Mar 08 '22 04:03 rversteegen

I use 7sfx files from: https://github.com/chrislake/7zsfxmm and the following approach:

  1. rcedit.exe 7zsd.sfx <options>
  2. copy /b 7zsd.sfx + sfx_config.txt + MyPackage.7z MyPackage.exe

You'r changing the icon of the SFX module which is in front of the archive.

Legend

fatherofinvention avatar Jun 15 '22 03:06 fatherofinvention