Subversion Repositories filter_foundry

Rev

Rev 416 | Rev 456 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.         This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.         Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
  4.         Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
  5.  
  6.         This program is free software; you can redistribute it and/or modify
  7.         it under the terms of the GNU General Public License as published by
  8.         the Free Software Foundation; either version 2 of the License, or
  9.         (at your option) any later version.
  10.  
  11.         This program is distributed in the hope that it will be useful,
  12.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.         GNU General Public License for more details.
  15.  
  16.         You should have received a copy of the GNU General Public License
  17.         along with this program; if not, write to the Free Software
  18.         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. #ifndef _CRT_SECURE_NO_WARNINGS
  22. #define _CRT_SECURE_NO_WARNINGS
  23. #endif
  24.  
  25. #include <iostream>
  26. #include <windows.h>
  27. #include <string>
  28. #include <fstream>
  29. #include <vector>
  30.  
  31. bool update_pe_timestamp(LPCTSTR filename, time_t timestamp) {
  32.         size_t peoffset;
  33.         FILE* fptr;
  34.  
  35.         fptr = _wfopen(filename, L"rb+");
  36.         if (fptr == NULL) return false;
  37.  
  38.         fseek(fptr, 0x3C, SEEK_SET);
  39.         fread(&peoffset, sizeof(peoffset), 1, fptr);
  40.  
  41.         fseek(fptr, (long)peoffset + 8, SEEK_SET);
  42.         fwrite(&timestamp, sizeof(time_t), 1, fptr);
  43.  
  44.         fclose(fptr);
  45.  
  46.         return true;
  47. }
  48.  
  49. bool update_pe_stackSizeCommit(LPCTSTR filename, size_t stackReserve, size_t stackCommit) {
  50.         size_t peoffset;
  51.         FILE* fptr;
  52.  
  53.         fptr = _wfopen(filename, L"rb+");
  54.         if (fptr == NULL) return false;
  55.  
  56.         fseek(fptr, 0x3C, SEEK_SET);
  57.         fread(&peoffset, sizeof(peoffset), 1, fptr);
  58.  
  59.         fseek(fptr, (long)peoffset + 12 * 8, SEEK_SET);
  60.         fwrite(&stackReserve, sizeof(size_t), 1, fptr);
  61.  
  62.         fseek(fptr, (long)peoffset + 12 * 8 + 4, SEEK_SET);
  63.         fwrite(&stackCommit, sizeof(size_t), 1, fptr);
  64.  
  65.         fclose(fptr);
  66.  
  67.         return true;
  68. }
  69.  
  70. bool update_pe_heapSizeCommit(LPCTSTR filename, size_t heapReserve, size_t heapCommit) {
  71.         size_t peoffset;
  72.         FILE* fptr;
  73.  
  74.         fptr = _wfopen(filename, L"rb+");
  75.         if (fptr == NULL) return false;
  76.  
  77.         fseek(fptr, 0x3C, SEEK_SET);
  78.         fread(&peoffset, sizeof(peoffset), 1, fptr);
  79.  
  80.         fseek(fptr, (long)peoffset + 13 * 8, SEEK_SET);
  81.         fwrite(&heapReserve, sizeof(size_t), 1, fptr);
  82.  
  83.         fseek(fptr, (long)peoffset + 13 * 8 + 4, SEEK_SET);
  84.         fwrite(&heapCommit, sizeof(size_t), 1, fptr);
  85.  
  86.         fclose(fptr);
  87.  
  88.         return true;
  89. }
  90.  
  91.  
  92. //DOS .EXE header
  93. struct image_dos_header
  94. {
  95.         uint16_t e_magic;                     // Magic number
  96.         uint16_t e_cblp;                      // Bytes on last page of file
  97.         uint16_t e_cp;                        // Pages in file
  98.         uint16_t e_crlc;                      // Relocations
  99.         uint16_t e_cparhdr;                   // Size of header in paragraphs
  100.         uint16_t e_minalloc;                  // Minimum extra paragraphs needed
  101.         uint16_t e_maxalloc;                  // Maximum extra paragraphs needed
  102.         uint16_t e_ss;                        // Initial (relative) SS value
  103.         uint16_t e_sp;                        // Initial SP value
  104.         uint16_t e_csum;                      // Checksum
  105.         uint16_t e_ip;                        // Initial IP value
  106.         uint16_t e_cs;                        // Initial (relative) CS value
  107.         uint16_t e_lfarlc;                    // File address of relocation table
  108.         uint16_t e_ovno;                      // Overlay number
  109.         uint16_t e_res[4];                    // Reserved words
  110.         uint16_t e_oemid;                     // OEM identifier (for e_oeminfo)
  111.         uint16_t e_oeminfo;                   // OEM information; e_oemid specific
  112.         uint16_t e_res2[10];                  // Reserved words
  113.         int32_t  e_lfanew;                    // File address of new exe header
  114. };
  115.  
  116. struct image_file_header
  117. {
  118.         uint16_t Machine;
  119.         uint16_t NumberOfSections;
  120.         uint32_t TimeDateStamp;
  121.         uint32_t PointerToSymbolTable;
  122.         uint32_t NumberOfSymbols;
  123.         uint16_t SizeOfOptionalHeader;
  124.         uint16_t Characteristics;
  125. };
  126.  
  127. uint32_t calculate_checksum(LPCTSTR filename) {
  128.         //Calculate checksum of image
  129.         // Taken from "PE Bliss" Cross-Platform Portable Executable C++ Library
  130.         // https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp
  131.         // Converted from C++ to C by Daniel Marschall
  132.  
  133.         FILE* fptr;
  134.         unsigned long long checksum = 0;
  135.         struct image_dos_header header;
  136.         size_t filesize;
  137.         unsigned long long top;
  138.         unsigned long pe_checksum_pos;
  139.         static const unsigned long checksum_pos_in_optional_headers = 64;
  140.         size_t i;
  141.  
  142.         fptr = _wfopen(filename, L"rb");
  143.         if (fptr == NULL) return 0x00000000;
  144.  
  145.         //Read DOS header
  146.         fseek(fptr, 0, SEEK_SET);
  147.         fread(&header, sizeof(struct image_dos_header), 1, fptr);
  148.  
  149.         //Calculate PE checksum
  150.         fseek(fptr, 0, SEEK_SET);
  151.         top = 0xFFFFFFFF;
  152.         top++;
  153.  
  154.         //"CheckSum" field position in optional PE headers - it's always 64 for PE and PE+
  155.         //Calculate real PE headers "CheckSum" field position
  156.         //Sum is safe here
  157.         pe_checksum_pos = header.e_lfanew + sizeof(struct image_file_header) + sizeof(uint32_t) + checksum_pos_in_optional_headers;
  158.  
  159.         //Calculate checksum for each byte of file
  160.         fseek(fptr, 0L, SEEK_END);
  161.         filesize = ftell(fptr);
  162.         fseek(fptr, 0L, SEEK_SET);
  163.         for (i = 0; i < filesize; i += 4)
  164.         {
  165.                 unsigned long dw = 0;
  166.  
  167.                 //Read DWORD from file
  168.                 fread(&dw, sizeof(dw), 1, fptr);
  169.                 //Skip "CheckSum" DWORD
  170.                 if (i == pe_checksum_pos)
  171.                         continue;
  172.  
  173.                 //Calculate checksum
  174.                 checksum = (checksum & 0xffffffff) + dw + (checksum >> 32);
  175.                 if (checksum > top)
  176.                         checksum = (checksum & 0xffffffff) + (checksum >> 32);
  177.         }
  178.  
  179.         //Finish checksum
  180.         checksum = (checksum & 0xffff) + (checksum >> 16);
  181.         checksum = (checksum)+(checksum >> 16);
  182.         checksum = checksum & 0xffff;
  183.  
  184.         checksum += (unsigned long)(filesize);
  185.  
  186.         fclose(fptr);
  187.  
  188.         //Return checksum
  189.         return (uint32_t)checksum;
  190. }
  191.  
  192. bool repair_pe_checksum(LPCTSTR filename) {
  193.         size_t peoffset;
  194.         FILE* fptr;
  195.  
  196.         uint32_t checksum = calculate_checksum(filename);
  197.         //if (checksum == 0x00000000) return false;
  198.  
  199.         fptr = _wfopen(filename, L"rb+");
  200.         if (fptr == NULL) return false;
  201.  
  202.         fseek(fptr, 0x3C, SEEK_SET);
  203.         fread(&peoffset, sizeof(peoffset), 1, fptr);
  204.  
  205.         fseek(fptr, (long)peoffset + 88, SEEK_SET);
  206.         fwrite(&checksum, sizeof(uint32_t), 1, fptr);
  207.  
  208.         fclose(fptr);
  209.  
  210.         return true;
  211. }
  212.  
  213. bool removeFromFile(LPCTSTR pluginfile, LPCTSTR lpType, LPCTSTR lpName, WORD wLanguage) {
  214.         bool bSuccessful = false;
  215.  
  216.         HANDLE hRes = BeginUpdateResource(pluginfile, false);
  217.         if (hRes != NULL) {
  218.                 if (UpdateResource(hRes, lpType, lpName, wLanguage, NULL, 0)) {
  219.                         if (EndUpdateResource(hRes, false)) {
  220.                                 bSuccessful = true;
  221.                         }
  222.                 }
  223.                 else {
  224.                         EndUpdateResource(hRes, true);
  225.                 }
  226.         }
  227.  
  228.         return bSuccessful;
  229. }
  230.  
  231. bool addToFile(LPCTSTR pluginfile, LPCTSTR otherfile, LPCTSTR lpType, LPCTSTR lpName, WORD wLanguage) {
  232.         HANDLE hFile;
  233.         DWORD dwFileSize, dwBytesRead;
  234.         LPBYTE lpBuffer;
  235.         bool bSuccessful = false;
  236.  
  237.         hFile = CreateFile(otherfile, GENERIC_READ,
  238.                 0,
  239.                 NULL,
  240.                 OPEN_EXISTING,
  241.                 FILE_ATTRIBUTE_NORMAL,
  242.                 NULL);
  243.  
  244.         if (INVALID_HANDLE_VALUE != hFile)
  245.         {
  246.                 dwFileSize = GetFileSize(hFile, NULL);
  247.  
  248.                 //lpBuffer = new BYTE[dwFileSize];
  249.                 lpBuffer = (LPBYTE)malloc(dwFileSize);
  250.  
  251.                 if (ReadFile(hFile, lpBuffer, dwFileSize, &dwBytesRead, NULL) != FALSE)
  252.                 {
  253.                         HANDLE hRes = BeginUpdateResource(pluginfile, false);
  254.                         if (hRes != NULL) {
  255.                                 if (UpdateResource(hRes, lpType, lpName, wLanguage, lpBuffer, dwFileSize)) {
  256.                                         if (EndUpdateResource(hRes, false)) {
  257.                                                 bSuccessful = true;
  258.                                         }
  259.                                 }
  260.                                 else {
  261.                                         EndUpdateResource(hRes, true);
  262.                                 }
  263.                         }
  264.                 }
  265.  
  266.                 //delete[] lpBuffer;
  267.                 free(lpBuffer);
  268.  
  269.                 CloseHandle(hFile);
  270.         }
  271.  
  272.         return bSuccessful;
  273. }
  274.  
  275. #ifdef UNICODE
  276. int binary_file_string_replace(std::wstring file_name, const char* asearch, const char* areplace) {
  277. #else
  278. int binary_file_string_replace(std::string file_name, const char* asearch, const char* areplace) {
  279. #endif
  280.         std::ifstream input(file_name, std::ios::binary);
  281.  
  282.         std::vector<char> buffer((std::istreambuf_iterator<char>(input)), (std::istreambuf_iterator<char>()));
  283.         std::vector<char>::iterator itbegin = buffer.begin();
  284.         std::vector<char>::iterator itend = buffer.end();
  285.  
  286.         if (strlen(asearch) != strlen(areplace)) {
  287.                 printf("Replace value length greater than original!\n");
  288.                 return -1;
  289.         }
  290.         int MAX_BUFFER = strlen(asearch);
  291.  
  292.         char* needed_str = (char*)malloc(MAX_BUFFER);
  293.         if (needed_str == 0) return -1;
  294.         char* replace_str = (char*)malloc(MAX_BUFFER);
  295.         if (replace_str == 0) return -1;
  296.        
  297.         memcpy(needed_str, asearch, MAX_BUFFER);
  298.         memcpy(replace_str, areplace, MAX_BUFFER);
  299.  
  300.         int ifound = 0;
  301.  
  302.         for (auto it = itbegin; it < itend ; it++) {
  303.                 if (memcmp(it._Ptr, needed_str, MAX_BUFFER) == 0) {
  304.                         strncpy(it._Ptr, replace_str, MAX_BUFFER);
  305.                         it += MAX_BUFFER - 1; // -1 because it++ will be set on the next loop
  306.                         ifound++;
  307.                 }
  308.         }
  309.  
  310.         if (ifound > 0) {
  311.                 std::ofstream ofile(file_name, std::ios::out | std::ios::binary);
  312.                 ofile.write((char*)&buffer[0], buffer.size() * sizeof(char));
  313.                 ofile.close();
  314.         }
  315.  
  316.         return ifound;
  317. }
  318.  
  319.  
  320. int main()
  321. {
  322.         LPCTSTR lpTemplateType = L"TPLT";
  323.         LPCTSTR lpName32Plugin = (LPCTSTR)1032;
  324.         LPCTSTR lpName64Plugin = (LPCTSTR)1064;
  325.         LPCTSTR lpName32Version = (LPCTSTR)3032;
  326.         LPCTSTR lpName64Version = (LPCTSTR)3064;
  327.         LPCTSTR lpName32Pipl = (LPCTSTR)16032;
  328.         LPCTSTR lpName64Pipl = (LPCTSTR)16064;
  329.         WORD wLanguageEnUs = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); // 1033 en-US
  330.         WORD wLanguageNeutral = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL); // 0 Neutral
  331.  
  332.         LPCTSTR file32in = L"in\\FilterFoundry.8bf";
  333.         LPCTSTR file64in = L"in\\FilterFoundry64.8bf";
  334.         LPCTSTR file32out = L"out\\FilterFoundry.8bf";
  335.         LPCTSTR file64out = L"out\\FilterFoundry64.8bf";
  336.         LPCTSTR file32tmp = L"FilterFoundry.tmp";
  337.         LPCTSTR file64tmp = L"FilterFoundry64.tmp";
  338.  
  339.         // 1a. Copy 32 "IN" to 32 "TMP", and 64 "IN" to 64 "TMP"
  340.         {
  341.                 if (!CopyFile(file32in, file32tmp, false)) {
  342.                         DeleteFile(file32out);
  343.                         DeleteFile(file64out);
  344.                         printf("Error: Copyfile 32in > 32tmp\n");
  345.                         return 1;
  346.                 }
  347.  
  348.                 if (!CopyFile(file64in, file64tmp, false)) {
  349.                         DeleteFile(file32out);
  350.                         DeleteFile(file64out);
  351.                         printf("Error: Copyfile 64in > 64tmp\n");
  352.                         return 1;
  353.                 }
  354.         }
  355.  
  356.         // 1b. Copy 32 "IN" to 32 "OUT", and 64 "IN" to 64 "OUT" (will be edited later)
  357.         {
  358.                 if (!CopyFile(file32in, file32out, false)) {
  359.                         DeleteFile(file32out);
  360.                         DeleteFile(file64out);
  361.                         printf("Error: Copyfile 32in > 32out\n");
  362.                         return 1;
  363.                 }
  364.  
  365.                 if (!CopyFile(file64in, file64out, false)) {
  366.                         DeleteFile(file32out);
  367.                         DeleteFile(file64out);
  368.                         printf("Error: Copyfile 64in > 64out\n");
  369.                         return 1;
  370.                 }
  371.         }
  372.  
  373.         // 2. Remove any template residues at 32/64 "TMP", since they are only used for building
  374.         //    "TMP" is our "standalone plugin skelleton"
  375.         // TODO: Also remove build dialogs, cursors and icons (like done in make_win.c)?
  376.         {
  377.                 // Remove TPLT 1 (Manifest template)
  378.                 removeFromFile(file32tmp, lpTemplateType, MAKEINTRESOURCE(1), wLanguageNeutral);
  379.                 removeFromFile(file64tmp, lpTemplateType, MAKEINTRESOURCE(1), wLanguageNeutral);
  380.  
  381.                 // Remove TPLT 1032/1064 (8BF included)
  382.                 removeFromFile(file32tmp, lpTemplateType, lpName32Plugin, wLanguageEnUs);
  383.                 removeFromFile(file32tmp, lpTemplateType, lpName64Plugin, wLanguageEnUs);
  384.                 removeFromFile(file64tmp, lpTemplateType, lpName32Plugin, wLanguageEnUs);
  385.                 removeFromFile(file64tmp, lpTemplateType, lpName64Plugin, wLanguageEnUs);
  386.  
  387.                 // Remove TPLT 3032/3064 (Versioninfo included)
  388.                 removeFromFile(file32tmp, lpTemplateType, lpName32Version, wLanguageEnUs);
  389.                 removeFromFile(file32tmp, lpTemplateType, lpName64Version, wLanguageEnUs);
  390.                 removeFromFile(file64tmp, lpTemplateType, lpName32Version, wLanguageEnUs);
  391.                 removeFromFile(file64tmp, lpTemplateType, lpName64Version, wLanguageEnUs);
  392.  
  393.                 // Remove TPLT 16032/16064 (PIPL template)
  394.                 removeFromFile(file32tmp, lpTemplateType, lpName32Pipl, wLanguageNeutral);
  395.                 removeFromFile(file32tmp, lpTemplateType, lpName64Pipl, wLanguageNeutral);
  396.                 removeFromFile(file64tmp, lpTemplateType, lpName32Pipl, wLanguageNeutral);
  397.                 removeFromFile(file64tmp, lpTemplateType, lpName64Pipl, wLanguageNeutral);
  398.         }
  399.  
  400.         // 32 bit (OpenWatcom cosmetics): Export table name "filterfoundry.dll" => "FilterFoundry.8bf"
  401.         // since OpenWatcom cannot link a 8BF file natively.
  402.         binary_file_string_replace(file32tmp, "filterfoundry.dll", "FilterFoundry.8bf");
  403.         binary_file_string_replace(file32out, "filterfoundry.dll", "FilterFoundry.8bf");
  404.  
  405.         // More OpenWatcom cosmetics! https://github.com/open-watcom/open-watcom-v2/issues/780 (Rejected)
  406.         // Stack reserved cannot be changed with linker option "OPTION STACK=1m" (Rejected)
  407.         // It is not required for DLLs, but everybody does it, and I think it is cosmetics to fill these fields, even if not required.
  408.         update_pe_stackSizeCommit(file32tmp, 1024 * 1024, 4096);
  409.         update_pe_stackSizeCommit(file32out, 1024 * 1024, 4096);
  410.         // Heap reserved can be changed with linker option "OPTION HEAP=1m"
  411.         update_pe_heapSizeCommit(file32tmp, 1024 * 1024, 4096);
  412.         update_pe_heapSizeCommit(file32out, 1024 * 1024, 4096);
  413.  
  414.         // 3. Update timestamp of 32/64 "TMP"
  415.         {
  416.                 if (!update_pe_timestamp(file32tmp, time(0))) {
  417.                         DeleteFile(file32out);
  418.                         DeleteFile(file64out);
  419.                         printf("Error: Update TMP timestamp 32\n");
  420.                         return 1;
  421.                 }
  422.  
  423.                 if (!update_pe_timestamp(file64tmp, time(0))) {
  424.                         DeleteFile(file32out);
  425.                         DeleteFile(file64out);
  426.                         printf("Error: Update TMP timestamp 64\n");
  427.                         return 1;
  428.                 }
  429.         }
  430.  
  431.         // 4. Repair checksums of 32/64 "TMP"
  432.         {
  433.                 if (!repair_pe_checksum(file32tmp)) {
  434.                         DeleteFile(file32out);
  435.                         DeleteFile(file64out);
  436.                         printf("Error: Repair TMP checksum 32\n");
  437.                         return 1;
  438.                 }
  439.                 if (!repair_pe_checksum(file64tmp)) {
  440.                         DeleteFile(file32out);
  441.                         DeleteFile(file64out);
  442.                         printf("Error: Repair TMP checksum 64\n");
  443.                         return 1;
  444.                 }
  445.         }
  446.  
  447.         // 6. Add 32/64 "TMP" to 64/32 "OUT" ("criss-cross")
  448.         {
  449.                 if (!addToFile(file32out, file32tmp, lpTemplateType, lpName32Plugin, wLanguageEnUs)) {
  450.                         DeleteFile(file32out);
  451.                         DeleteFile(file64out);
  452.                         printf("Error: Add 32 to 32\n");
  453.                         return 1;
  454.                 }
  455.                 if (!addToFile(file32out, file64tmp, lpTemplateType, lpName64Plugin, wLanguageEnUs)) {
  456.                         DeleteFile(file32out);
  457.                         DeleteFile(file64out);
  458.                         printf("Error: Add 64 to 32\n");
  459.                         return 1;
  460.                 }
  461.  
  462.                 if (!addToFile(file64out, file32tmp, lpTemplateType, lpName32Plugin, wLanguageEnUs)) {
  463.                         DeleteFile(file32out);
  464.                         DeleteFile(file64out);
  465.                         printf("Error: Add 32 to 64\n");
  466.                         return 1;
  467.                 }
  468.  
  469.                 if (!addToFile(file64out, file64tmp, lpTemplateType, lpName64Plugin, wLanguageEnUs)) {
  470.                         DeleteFile(file32out);
  471.                         DeleteFile(file64out);
  472.                         printf("Error: Add 64 to 64\n");
  473.                         return 1;
  474.                 }
  475.         }
  476.  
  477.         // 7a. Read Version Info from 32 bit "TMP", and copy it to 32/64 "OUT" template
  478.         {
  479.                 HMODULE lib = LoadLibraryEx(file32tmp, NULL, LOAD_LIBRARY_AS_DATAFILE);
  480.                 if (!lib) {
  481.                         printf("Loadlib failed at versioninfo TPLT 32");
  482.                         return 1;
  483.                 }
  484.                 HRSRC resinfo = FindResource(lib, MAKEINTRESOURCE(1), RT_VERSION);
  485.                 if (!resinfo) {
  486.                         printf("FindResource failed at versioninfo TPLT 32");
  487.                         return 1;
  488.                 }
  489.                 size_t cbVersionInfo = SizeofResource(lib, resinfo);
  490.                 HGLOBAL hvinfo = LoadResource(lib, resinfo);
  491.                 if (!hvinfo) {
  492.                         printf("LoadResource failed at versioninfo TPLT 32");
  493.                         return 1;
  494.                 }
  495.                 char* vinfo = (char*)LockResource(hvinfo);
  496.                 char* vinfocpy = (char*)malloc(cbVersionInfo);
  497.                 if (vinfocpy == NULL) return 1;
  498.                 memcpy(vinfocpy, vinfo, cbVersionInfo);
  499.                 UnlockResource(hvinfo);
  500.                 FreeLibrary(lib);
  501.  
  502.                 // Write Version info to TPLT Resource of 32 bit "OUT"
  503.  
  504.                 HANDLE hupd = BeginUpdateResource(file32out, false);
  505.                 UpdateResource(hupd, TEXT("TPLT"), lpName32Version, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), vinfocpy, cbVersionInfo);
  506.                 EndUpdateResource(hupd, false);
  507.  
  508.                 // Write Version info to TPLT Resource of 64 bit "OUT"
  509.  
  510.                 hupd = BeginUpdateResource(file64out, false);
  511.                 UpdateResource(hupd, TEXT("TPLT"), lpName32Version, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), vinfocpy, cbVersionInfo);
  512.                 EndUpdateResource(hupd, false);
  513.  
  514.                 // Free memory
  515.  
  516.                 free(vinfocpy);
  517.         }
  518.  
  519.         // 7b. Read Version Info from 64 bit "TMP", and copy it to 32/64 "OUT" template
  520.         {
  521.                 HMODULE lib = LoadLibraryEx(file64tmp, NULL, LOAD_LIBRARY_AS_DATAFILE);
  522.                 if (!lib) {
  523.                         printf("Loadlib failed at versioninfo TPLT 64");
  524.                         return 1;
  525.                 }
  526.                 HRSRC resinfo = FindResource(lib, MAKEINTRESOURCE(1), RT_VERSION);
  527.                 if (!resinfo) {
  528.                         printf("FindResource failed at versioninfo TPLT 64");
  529.                         return 1;
  530.                 }
  531.                 size_t cbVersionInfo = SizeofResource(lib, resinfo);
  532.                 HGLOBAL hvinfo = LoadResource(lib, resinfo);
  533.                 if (!hvinfo) {
  534.                         printf("LoadResource failed at versioninfo TPLT 64");
  535.                         return 1;
  536.                 }
  537.                 char* vinfo = (char*)LockResource(hvinfo);
  538.                 char* vinfocpy = (char*)malloc(cbVersionInfo);
  539.                 if (vinfocpy == NULL) return 1;
  540.                 memcpy(vinfocpy, vinfo, cbVersionInfo);
  541.                 UnlockResource(hvinfo);
  542.                 FreeLibrary(lib);
  543.  
  544.                 // Write Version info to TPLT Resource of 32 bit "OUT"
  545.  
  546.                 HANDLE hupd = BeginUpdateResource(file32out, false);
  547.                 UpdateResource(hupd, TEXT("TPLT"), lpName64Version, 1033, vinfocpy, cbVersionInfo);
  548.                 EndUpdateResource(hupd, false);
  549.  
  550.                 // Write Version info to TPLT Resource of 64 bit "OUT"
  551.  
  552.                 hupd = BeginUpdateResource(file64out, false);
  553.                 UpdateResource(hupd, TEXT("TPLT"), lpName64Version, 1033, vinfocpy, cbVersionInfo);
  554.                 EndUpdateResource(hupd, false);
  555.  
  556.                 // Free memory
  557.  
  558.                 free(vinfocpy);
  559.         }
  560.  
  561.         // 8. Delete 32/64 "TMP"
  562.         {
  563.                 DeleteFile(file32tmp);
  564.                 DeleteFile(file64tmp);
  565.         }
  566.  
  567.         // 9. Update timestamp of 32/64 "OUT"
  568.         {
  569.                 if (!update_pe_timestamp(file32out, time(0))) {
  570.                         DeleteFile(file32out);
  571.                         DeleteFile(file64out);
  572.                         printf("Error: Update OUT timestamp 32\n");
  573.                         return 1;
  574.                 }
  575.  
  576.                 if (!update_pe_timestamp(file64out, time(0))) {
  577.                         DeleteFile(file32out);
  578.                         DeleteFile(file64out);
  579.                         printf("Error: Update OUT timestamp 64\n");
  580.                         return 1;
  581.                 }
  582.         }
  583.  
  584.         // 10. Repair checksums of 32/64 "OUT"
  585.         {
  586.                 if (!repair_pe_checksum(file32out)) {
  587.                         DeleteFile(file32out);
  588.                         DeleteFile(file64out);
  589.                         printf("Error: Repair OUT checksum 32\n");
  590.                         return 1;
  591.                 }
  592.                 if (!repair_pe_checksum(file64out)) {
  593.                         DeleteFile(file32out);
  594.                         DeleteFile(file64out);
  595.                         printf("Error: Repair OUT checksum 64\n");
  596.                         return 1;
  597.                 }
  598.         }
  599.  
  600.         // 11. All done!
  601.  
  602.         printf("All OK!\n");
  603.         return 0;
  604. }
  605.