Subversion Repositories filter_foundry

Rev

Rev 536 | 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.net
  4.     Copyright (C) 2018-2022 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. #include "ff.h"
  22.  
  23. #include <time.h>
  24.  
  25. #include "file_compat.h"
  26. #include "compat_string.h"
  27. #include "compat_win.h"
  28. #include "versioninfo_modify_win.h"
  29. #include "version.h"
  30.  
  31. extern HINSTANCE hDllInstance;
  32.  
  33. typedef struct _PE32 {
  34.         uint32_t magic; // 0x50450000
  35.         IMAGE_FILE_HEADER fileHeader; // COFF Header without Signature
  36.         IMAGE_OPTIONAL_HEADER32 optHeader; // Standard COFF fields, Windows Specific Fields, Data Directories
  37. } PE32;
  38.  
  39. Boolean doresources(FSSpec* dst, int bits);
  40.  
  41. void showLastError(TCHAR *func){
  42.         TCHAR s[0x300] = {0};
  43.  
  44.         xstrcpy(&s[0],func);
  45.         xstrcat(&s[0],TEXT(" failed: ")); // TODO (Not so important): TRANSLATE
  46.         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, s + xstrlen(s), 0x300 - (DWORD)xstrlen(s), NULL);
  47.         simplealert(&s[0]);
  48. }
  49.  
  50. /*
  51. BOOL CALLBACK enumfunc(HMODULE hModule,LPCTSTR lpszType,LPCTSTR lpszName,WORD wIDLanguage,LONG lParam){
  52.         #ifdef DEBUG
  53.         char s[0x100];
  54.         sprintf(s,"EnumResourceLanguages callback: module=%#x type=%s name=%s lang=%d",
  55.                 hModule,lpszType,lpszName,wIDLanguage);
  56.         dbg(s);
  57.         #endif
  58.         return TRUE;
  59. }
  60. */
  61.  
  62. int WriteXmlEscaped(char* description, char c) {
  63.         int idescription = 0;
  64.         if (c == '&') {
  65.                 description[idescription++] = '&';
  66.                 description[idescription++] = 'a';
  67.                 description[idescription++] = 'm';
  68.                 description[idescription++] = 'p';
  69.                 description[idescription++] = ';';
  70.         }
  71.         else if (c == '<') {
  72.                 description[idescription++] = '&';
  73.                 description[idescription++] = 'l';
  74.                 description[idescription++] = 't';
  75.                 description[idescription++] = ';';
  76.         }
  77.         else if (c == '>') {
  78.                 description[idescription++] = '&';
  79.                 description[idescription++] = 'g';
  80.                 description[idescription++] = 't';
  81.                 description[idescription++] = ';';
  82.         }
  83.         else {
  84.                 description[idescription++] = c;
  85.         }
  86.         return idescription;
  87. }
  88.  
  89. int domanifest(char *newmanifest, char *manifestp, PARM_T* pparm, int bits) {
  90.         char* name;
  91.         char* description, *tmpDescription;
  92.         int res;
  93.         size_t i;
  94.         size_t iname;
  95.  
  96.         name = (char*)malloc(40 + (2 * 256) * 5);
  97.         description = (char*)malloc(10 + (2 * 256)); // x4 because & becomes &amp;
  98.         if (name == NULL || description == NULL) return 0;
  99.  
  100.         // Description
  101.         tmpDescription = description;
  102.         for (i = 0; i < strlen(pparm->szCategory); i++) {
  103.                 char c = pparm->szCategory[i];
  104.                 tmpDescription += WriteXmlEscaped(tmpDescription, c);
  105.         }
  106.         tmpDescription += WriteXmlEscaped(tmpDescription, ' ');
  107.         tmpDescription += WriteXmlEscaped(tmpDescription, '-');
  108.         tmpDescription += WriteXmlEscaped(tmpDescription, ' ');
  109.         for (i = 0; i < strlen(pparm->szTitle); i++) {
  110.                 char c = pparm->szTitle[i];
  111.                 tmpDescription += WriteXmlEscaped(tmpDescription, c);
  112.         }
  113.         tmpDescription[0] = '\0';
  114.  
  115.         // Name
  116.         strcpy(name, "Telegraphics.FilterFoundry.");
  117.         iname = strlen("Telegraphics.FilterFoundry.");
  118.         for (i = 0; i < strlen(pparm->szCategory); i++) {
  119.                 char c = pparm->szCategory[i];
  120.                 if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'))) {
  121.                         name[iname++] = c;
  122.                 }
  123.         }
  124.         name[iname++] = '.';
  125.         for (i = 0; i < strlen(pparm->szTitle); i++) {
  126.                 char c = pparm->szTitle[i];
  127.                 if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'))) {
  128.                         name[iname++] = c;
  129.                 }
  130.         }
  131.         name[iname++] = '\0';
  132.  
  133.         if (bits == 64) {
  134.                 res = sprintf(newmanifest, manifestp, (char*)name, "amd64", VERSION_STR, (char*)description);
  135.         }
  136.         else {
  137.                 res = sprintf(newmanifest, manifestp, (char*)name, "x86", VERSION_STR, (char*)description);
  138.         }
  139.  
  140.         free(name);
  141.         free(description);
  142.  
  143.         return res;
  144. }
  145.  
  146. ULONG changeVersionInfo(FSSpec* dst, HANDLE hUpdate, PARM_T* pparm, int bits) {
  147.         LPTSTR soleFilename;
  148.         LPWSTR changeRequestStrW, tmp;
  149.         ULONG dwError = NOERROR;
  150.         HRSRC hResInfo;
  151.         HGLOBAL hg;
  152.         ULONG size;
  153.         PVOID pv;
  154.         //BOOL fDiscard = TRUE;
  155.  
  156.         if (soleFilename = xstrrchr(&dst->szName[0], '\\')) {
  157.                 ++soleFilename;
  158.         }
  159.         else {
  160.                 soleFilename = &dst->szName[0];
  161.         }
  162.  
  163.         // Format of argument "PCWSTR changes" is "<name>\0<value>\0<name>\0<value>\0....."
  164.         // You can CHANGE values for any given name
  165.         // You can DELETE entries by setting the value to "\b" (0x08 backspace character)
  166.         // You cannot (yet) ADD entries.
  167.         changeRequestStrW = (LPWSTR)malloc((6 * 2 * 100 + 1) * sizeof(WCHAR));
  168.         if (changeRequestStrW == 0) return E_OUTOFMEMORY;
  169.         memset((char*)changeRequestStrW, 0, sizeof(changeRequestStrW));
  170.  
  171.         tmp = changeRequestStrW;
  172.  
  173.         tmp += mbstowcs(tmp, "Comments", 100);
  174.         tmp++;
  175.         tmp += mbstowcs(tmp, "Built using Filter Foundry " VERSION_STR, 100);
  176.         tmp++;
  177.  
  178.         tmp += mbstowcs(tmp, "CompanyName", 100);
  179.         tmp++;
  180.         if (strlen(pparm->szAuthor) > 0) {
  181.                 tmp += mbstowcs(tmp, pparm->szAuthor, 100);
  182.         }
  183.         else {
  184.                 tmp += mbstowcs(tmp, "\b", 100); // \b = remove
  185.         }
  186.         tmp++;
  187.  
  188.         tmp += mbstowcs(tmp, "LegalCopyright", 100);
  189.         tmp++;
  190.         if (strlen(pparm->szCopyright) > 0) {
  191.                 tmp += mbstowcs(tmp, pparm->szCopyright, 100);
  192.         }
  193.         else {
  194.                 tmp += mbstowcs(tmp, "\b", 100); // \b = remove
  195.         }
  196.         tmp++;
  197.  
  198.         tmp += mbstowcs(tmp, "FileDescription", 100);
  199.         tmp++;
  200.         if (strlen(pparm->szTitle) > 0) {
  201.                 tmp += mbstowcs(tmp, pparm->szTitle, 100);
  202.         }
  203.         else {
  204.                 tmp += mbstowcs(tmp, "Untitled filter", 100);
  205.         }
  206.         tmp++;
  207.  
  208.         tmp += mbstowcs(tmp, "OriginalFilename", 100);
  209.         tmp++;
  210.         #ifdef UNICODE
  211.         xstrcpy(tmp, soleFilename);
  212.         tmp += xstrlen(soleFilename);
  213.         #else
  214.         tmp += mbstowcs(tmp, soleFilename, 100);
  215.         #endif
  216.         tmp++;
  217.  
  218.         tmp += mbstowcs(tmp, "License", 100);
  219.         tmp++;
  220.         tmp += mbstowcs(tmp, "\b", 100); // \b = remove, since filter is standalone and might have its own license
  221.         tmp++;
  222.  
  223.         tmp += mbstowcs(tmp, "", 1);
  224.  
  225.         if (hResInfo = FindResourceEx(hDllInstance, TEXT("TPLT"), MAKEINTRESOURCE(3000 + bits), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)))
  226.         {
  227.                 if (hg = LoadResource(hDllInstance, hResInfo))
  228.                 {
  229.                         if (size = SizeofResource(hDllInstance, hResInfo))
  230.                         {
  231.                                 if (pv = LockResource(hg))
  232.                                 {
  233.                                         if (UpdateVersionRaw(pv, size, &pv, &size, changeRequestStrW))
  234.                                         {
  235.                                                 if (_UpdateResource(hUpdate, RT_VERSION, MAKEINTRESOURCE(1), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), pv, size))
  236.                                                 {
  237.                                                         //fDiscard = FALSE;
  238.                                                 }
  239.                                                 else {
  240.                                                         //dwError = GetLastError();
  241.                                                 }
  242.                                         }
  243.                                         LocalFree(pv);
  244.                                 }
  245.                         }
  246.                 }
  247.         }
  248.  
  249.         free(changeRequestStrW);
  250.  
  251.         return dwError;
  252. }
  253.  
  254. typedef long __time32_t;
  255.  
  256. Boolean update_pe_timestamp(const FSSpec* dst, __time32_t timestamp) {
  257.         size_t peoffset;
  258.         FILEREF fptr;
  259.         Boolean res;
  260.         FILECOUNT cnt;
  261.  
  262.         if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return false;
  263.  
  264.         res =
  265.                 SetFPos(fptr, fsFromStart, 0x3C) ||
  266.                 (cnt = sizeof(peoffset), noErr) ||
  267.                 FSRead(fptr, &cnt, &peoffset) ||
  268.                 SetFPos(fptr, fsFromStart, (long)peoffset + /*0x0008*/offsetof(PE32, fileHeader.TimeDateStamp)) ||
  269.                 (cnt = sizeof(__time32_t), noErr) ||
  270.                 FSWrite(fptr, &cnt, &timestamp);
  271.  
  272.         FSClose(fptr);
  273.  
  274.         return res == noErr; // res=0 means everything was noErr, res=1 means something was !=noErr
  275. }
  276.  
  277. uint32_t calculate_checksum(FSSpec* dst) {
  278.         //Calculate checksum of image
  279.         // Taken from "PE Bliss" Cross-Platform Portable Executable C++ Library
  280.         // https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp
  281.         // Converted from C++ to C by Daniel Marschall
  282.  
  283.         FILEREF fptr;
  284.         unsigned long long checksum = 0;
  285.         IMAGE_DOS_HEADER header;
  286.         FILEPOS filesize, i;
  287.         unsigned long long top;
  288.         unsigned long pe_checksum_pos;
  289.         static const unsigned long checksum_pos_in_optional_headers = 64;
  290.         FILECOUNT cnt;
  291.  
  292.         if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return 0x00000000;
  293.  
  294.         //Read DOS header
  295.         SetFPos(fptr, fsFromStart, 0);
  296.         cnt = sizeof(IMAGE_DOS_HEADER);
  297.         FSRead(fptr, &cnt, &header);
  298.  
  299.         //Calculate PE checksum
  300.         SetFPos(fptr, fsFromStart, 0);
  301.         top = 0xFFFFFFFF;
  302.         top++;
  303.  
  304.         //"CheckSum" field position in optional PE headers - it's always 64 for PE and PE+
  305.         //Calculate real PE headers "CheckSum" field position
  306.         //Sum is safe here
  307.         pe_checksum_pos = header.e_lfanew + sizeof(IMAGE_FILE_HEADER) + sizeof(uint32_t) + checksum_pos_in_optional_headers;
  308.  
  309.         //Calculate checksum for each byte of file
  310.         filesize = 0;
  311.         GetEOF(fptr, &filesize);
  312.         SetFPos(fptr, fsFromStart, 0);
  313.         for (i = 0; i < filesize; i += 4)
  314.         {
  315.                 unsigned long dw = 0;
  316.  
  317.                 //Read DWORD from file
  318.                 cnt = sizeof(dw);
  319.                 FSRead(fptr, &cnt, &dw);
  320.                 //Skip "CheckSum" DWORD
  321.                 if (i == pe_checksum_pos)
  322.                         continue;
  323.  
  324.                 //Calculate checksum
  325.                 checksum = (checksum & 0xffffffff) + dw + (checksum >> 32);
  326.                 if (checksum > top)
  327.                         checksum = (checksum & 0xffffffff) + (checksum >> 32);
  328.         }
  329.  
  330.         //Finish checksum
  331.         checksum = (checksum & 0xffff) + (checksum >> 16);
  332.         checksum = (checksum)+(checksum >> 16);
  333.         checksum = checksum & 0xffff;
  334.  
  335.         checksum += (unsigned long)(filesize);
  336.  
  337.         FSClose(fptr);
  338.  
  339.         //Return checksum
  340.         return (uint32_t)checksum;
  341. }
  342.  
  343. Boolean repair_pe_checksum(FSSpec* dst) {
  344.         size_t peoffset;
  345.         FILEREF fptr;
  346.         FILECOUNT cnt;
  347.         Boolean res;
  348.  
  349.         uint32_t checksum = calculate_checksum(dst);
  350.         //if (checksum == 0x00000000) return false;
  351.  
  352.         if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return false;
  353.  
  354.         res =
  355.                 SetFPos(fptr, fsFromStart, 0x3C) ||
  356.                 (cnt = sizeof(peoffset), noErr) ||
  357.                 FSRead(fptr, &cnt, &peoffset) ||
  358.                 SetFPos(fptr, fsFromStart, (long)peoffset + /*0x0058*/offsetof(PE32, optHeader.CheckSum)) ||
  359.                 (cnt = sizeof(uint32_t), noErr) ||
  360.                 FSWrite(fptr, &cnt, &checksum);
  361.  
  362.         FSClose(fptr);
  363.  
  364.         return res == noErr; // res=0 means everything was noErr, res=1 means something was !=noErr
  365. }
  366.  
  367. typedef struct {
  368.         char funcname[8];
  369.         uint16_t codelen;
  370. } operdef_t;
  371.  
  372. typedef struct {
  373.         char funcname[8];
  374.         uint16_t numparams;
  375. } funcdef_t;
  376.  
  377. typedef struct {
  378.         char funcname[8];
  379.         char referencename[8];
  380. } symndef_t;
  381.  
  382. Boolean doresources(FSSpec* dst, int bits){
  383.         HRSRC datarsrc,aetersrc,manifestsrc;
  384.         HGLOBAL datah,aeteh,hupdate,manifesth;
  385.  
  386.         operdef_t dummy_oper;
  387.         funcdef_t dummy_func;
  388.         symndef_t dummy_symn;
  389.  
  390.         Ptr newpipl = NULL, newaete = NULL;
  391.         LPVOID datap, aetep, manifestp;
  392.         char* manifestp_copy;
  393.         PARM_T *pparm = NULL;
  394.         size_t piplsize,aetesize,origsize;
  395.         char title[256], category[256];
  396.         LPCTSTR parm_type;
  397.         LPCTSTR parm_id;
  398.         Boolean discard = true;
  399.         uint64_t obfuscseed = 0, obfuscseed2 = 0;
  400.         long event_id = 0;
  401.  
  402.         memset(&dummy_oper, 0, sizeof(operdef_t));
  403.         memset(&dummy_func, 0, sizeof(funcdef_t));
  404.         memset(&dummy_symn, 0, sizeof(symndef_t));
  405.  
  406.         if( (hupdate = _BeginUpdateResource(&dst->szName[0],false)) ){
  407.                 if( (datarsrc = FindResource(hDllInstance,MAKEINTRESOURCE(16000 + bits), TEXT("TPLT")))
  408.                         && (datah = LoadResource(hDllInstance,datarsrc))
  409.                         && (datap = (Ptr)LockResource(datah))
  410.                         && (aetersrc = FindResource(hDllInstance, MAKEINTRESOURCE(16000), TEXT("AETE")))
  411.                         && (aeteh = LoadResource(hDllInstance, aetersrc))
  412.                         && (aetep = (Ptr)LockResource(aeteh))
  413.                         && (manifestsrc = FindResource(hDllInstance, MAKEINTRESOURCE(1), TEXT("TPLT")))
  414.                         && (manifesth = LoadResource(hDllInstance, manifestsrc))
  415.                         && (manifestp = (Ptr)LockResource(manifesth)) )
  416.                 {
  417.                         char* newmanifest;
  418.                         int manifestsize = SizeofResource(hDllInstance, manifestsrc);
  419.  
  420.                         newmanifest = (char*)malloc((size_t)manifestsize + 4096/*+4KiB for name,description,etc.*/);
  421.  
  422.                         strcpy_win_replace_ampersand(&title[0], &gdata->parm.szTitle[0]);
  423.                         if (gdata->parm.popDialog)
  424.                                 strcat(title, "...");
  425.  
  426.                         strcpy_win_replace_ampersand(&category[0], &gdata->parm.szCategory[0]);
  427.  
  428.                         origsize = SizeofResource(hDllInstance,datarsrc);
  429.  
  430.                         if( (newpipl = (Ptr)malloc(origsize+0x300))
  431.                          && (newaete = (Ptr)malloc(4096))
  432.                          && (pparm = (PARM_T*)malloc(sizeof(PARM_T))) )
  433.                         {
  434.                                 // ====== Generate AETE and PIPL
  435.  
  436.                                 /* add user-specified title and category to new PiPL */
  437.                                 memcpy(newpipl,datap,origsize);
  438.                                 /* note that Windows PiPLs have 2 byte version datum in front
  439.                                    that isn't reflected in struct definition or Mac resource template: */
  440.                                 piplsize = fixpipl((PIPropertyList*)(newpipl+2),origsize-2,&title[0],&gdata->parm.szTitle[0],&category[0],&event_id) + 2;
  441.  
  442.                                 /* set up the PARM resource with saved parameters */
  443.                                 memcpy(pparm,&gdata->parm,sizeof(PARM_T));
  444.  
  445.                                 /* Generate 'aete' resource (contains names of the parameters for the "Actions" tab in Photoshop) */
  446.                                 aetesize = aete_generate(newaete, pparm, event_id);
  447.  
  448.                                 // ====== Create fitting manifest for the activation context
  449.  
  450.                                 manifestp_copy = (char*)malloc((size_t)manifestsize + 1/*sz*/);
  451.                                 if (manifestp_copy != 0) {
  452.                                         memcpy(manifestp_copy, manifestp, manifestsize); // copy manifestp to manifestp_copy, because manifestp is readonly
  453.                                         manifestp_copy[manifestsize] = '\0'; // and add the null-terminating char, because domanifest() uses sprintf() on it
  454.                                         manifestsize = domanifest(newmanifest, manifestp_copy, pparm, bits);
  455.                                         free(manifestp_copy);
  456.                                 }
  457.  
  458.                                 // ====== Change version attributes
  459.  
  460.                                 if (changeVersionInfo(dst, hupdate, pparm, bits) != NOERROR) {
  461.                                         simplewarning((TCHAR*)TEXT("changeVersionInfo failed")); // TODO (Not so important): TRANSLATE
  462.                                 }
  463.  
  464.                                 // ====== Obfuscate pparm!
  465.  
  466.                                 if (gdata->obfusc) {
  467.                                         parm_type = OBFUSCDATA_TYPE_NEW;
  468.                                         parm_id = OBFUSCDATA_ID_NEW;
  469.  
  470.                                         // Note: After we have finished updating the resources, we will write <obfuscseed> into the binary code of the 8BF file
  471.                                         obfusc(pparm, &obfuscseed, &obfuscseed2);
  472.                                 }else{
  473.                                         parm_type = PARM_TYPE;
  474.                                         parm_id = PARM_ID_NEW;
  475.                                 }
  476.  
  477.                                 // ====== Save AETE, PIPL, Manifest and PARM/RCDATA
  478.  
  479.                                 /* Attention: The resource we have found using FindResource() might have a different
  480.                                    language than the resource we are saving (Neutral), so we might end up having
  481.                                    multiple languages for the same resource. Therefore, the language "Neutral" was
  482.                                    set in the Scripting.rc file for the resource AETE and PIPL.rc for the resources PIPL. */
  483.  
  484.                                 if(
  485.                                            _UpdateResource(hupdate, RT_DIALOG, MAKEINTRESOURCE(ID_BUILDDLG), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), NULL, 0) // clean up things we don't need in the standalone plugin
  486.                                         && _UpdateResource(hupdate, RT_DIALOG, MAKEINTRESOURCE(ID_BUILDDLG), MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN), NULL, 0) // clean up things we don't need in the standalone plugin
  487.                                         && _UpdateResource(hupdate, RT_DIALOG, MAKEINTRESOURCE(ID_MAINDLG), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), NULL, 0) // clean up things we don't need in the standalone plugin
  488.                                         && _UpdateResource(hupdate, RT_DIALOG, MAKEINTRESOURCE(ID_MAINDLG), MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN), NULL, 0) // clean up things we don't need in the standalone plugin
  489.                                         && _UpdateResource(hupdate, RT_GROUP_ICON, TEXT("CAUTION_ICO"), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), NULL, 0) // clean up things we don't need in the standalone plugin
  490. //                                      && _UpdateResource(hupdate, RT_ICON, MAKEINTRESOURCE(1)/*Caution*/, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), NULL, 0) // clean up things we don't need in the standalone plugin
  491.                                         && _UpdateResource(hupdate, RT_GROUP_CURSOR, TEXT("HAND_QUESTION"), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), NULL, 0) // clean up things we don't need in the standalone plugin
  492.                                         // TODO: Removing the single resources don't work correctly. Sometimes the cursors are numbered 4,5,6 and sometimes 1,2,3 . Probably conflicts with icons
  493. //                                      && _UpdateResource(hupdate, RT_CURSOR, MAKEINTRESOURCE(3)/*QuestionHand*/, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), NULL, 0) // clean up things we don't need in the standalone plugin
  494.                                         && _UpdateResource(hupdate, TEXT("PIPL") /* note: caps!! */, MAKEINTRESOURCE(16000), MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),newpipl,(DWORD)piplsize)
  495.                                         && _UpdateResource(hupdate, TEXT("AETE") /* note: caps!! */, MAKEINTRESOURCE(16000), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), newaete, (DWORD)aetesize)
  496.                                         // OPER and FUNC are written so that "Plugin Manager 2.1" thinks that this plugin is a Filter Factory plugin! SYNM is not important, though.
  497.                                         && (gdata->obfusc || _UpdateResource(hupdate, TEXT("OPER"), MAKEINTRESOURCE(16000), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), &dummy_oper, sizeof(dummy_oper)))
  498.                                         && (gdata->obfusc || _UpdateResource(hupdate, TEXT("FUNC"), MAKEINTRESOURCE(16000), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), &dummy_func, sizeof(dummy_func)))
  499.                                         && (gdata->obfusc || _UpdateResource(hupdate, TEXT("SYNM"), MAKEINTRESOURCE(16000), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), &dummy_symn, sizeof(dummy_symn)))
  500.                                         && _UpdateResource(hupdate, RT_MANIFEST, MAKEINTRESOURCE(1), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), newmanifest, (DWORD)manifestsize)
  501.                                         && _UpdateResource(hupdate, parm_type,parm_id, MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),pparm,sizeof(PARM_T)) )
  502.                                 {
  503.                                         discard = false;
  504.                                 } else {
  505.                                         showLastError((TCHAR*)TEXT("UpdateResource"));
  506.                                 }
  507.                         }
  508.  
  509.                         free(newmanifest);
  510.  
  511.                         // Here, the file will be saved
  512.                         if (_EndUpdateResource(hupdate, discard)) {
  513.                                 if (gdata->obfusc) {
  514.                                         // We modify the binary code to replace the deobfuscate-seed from <cObfuscSeed> to <obfuscseed>
  515.  
  516.                                         // First try with alignment "4" (this should be the usual case),
  517.                                         // and if that failed, try without alignment ("1").
  518.                                         // We only need to set maxamount to "1", because "const volatile" makes sure that
  519.                                         // the compiler won't place (inline) it at several locations in the code.
  520.                                         if (!obfusc_seed_replace(dst, GetObfuscSeed(), GetObfuscSeed2(), obfuscseed, obfuscseed2, 1, 1))
  521.                                         {
  522.                                                 simplewarning((TCHAR*)TEXT("obfusc_seed_replace failed")); // TODO (Not so important): TRANSLATE
  523.                                                 discard = true;
  524.                                         }
  525.                                 }
  526.  
  527.                                 if (!update_pe_timestamp(dst, (__time32_t)time(0))) {
  528.                                         simplewarning((TCHAR*)TEXT("update_pe_timestamp failed")); // TODO (Not so important): TRANSLATE
  529.                                 }
  530.  
  531.                                 if (!repair_pe_checksum(dst)) {
  532.                                         simplewarning((TCHAR*)TEXT("repair_pe_checksum failed")); // TODO (Not so important): TRANSLATE
  533.                                 }
  534.                         }else showLastError((TCHAR*)TEXT("EndUpdateResource"));
  535.  
  536.                 }else showLastError((TCHAR*)TEXT("Find-, Load- or LockResource"));
  537.  
  538.                 if(pparm) free(pparm);
  539.                 if(newpipl) free(newpipl);
  540.                 if(newaete) free(newaete);
  541.         }else
  542.         showLastError((TCHAR*)TEXT("BeginUpdateResource"));
  543.         return !discard;
  544. }
  545.  
  546. Boolean remove_64_filename_prefix(LPTSTR dstname) {
  547.         // foobar.8bf => foobar.8bf
  548.         // foobar64.8bf => foobar.8bf
  549.         size_t i;
  550.         for (i = xstrlen(dstname); i > 2; i--) {
  551.                 if (dstname[i] == '.') {
  552.                         if ((dstname[i - 2] == '6') && (dstname[i - 1] == '4')) {
  553.                                 size_t tmp = xstrlen(dstname);
  554.                                 memcpy(&dstname[i - 2], &dstname[i], (xstrlen(dstname) - i + 1) * sizeof(TCHAR));
  555.                                 dstname[tmp - 2] = 0;
  556.                                 return true;
  557.                         }
  558.                 }
  559.         }
  560.         return false;
  561. }
  562.  
  563. Boolean add_64_filename_prefix(LPTSTR dstname) {
  564.         // foobar.8bf => foobar64.8bf
  565.         size_t i;
  566.         for (i = xstrlen(dstname); i > 2; i--) {
  567.                 if (dstname[i] == '.') {
  568.                         size_t tmp = xstrlen(dstname);
  569.                         memcpy(&dstname[i + 2], &dstname[i], (xstrlen(dstname) - i + 1) * sizeof(TCHAR));
  570.                         dstname[i] = '6';
  571.                         dstname[i + 1] = '4';
  572.                         dstname[tmp + 2] = 0;
  573.                         return true;
  574.                 }
  575.         }
  576.         return false;
  577. }
  578.  
  579. BOOL FileExists(LPCTSTR szPath) {
  580.         DWORD dwAttrib = GetFileAttributes(szPath);
  581.         return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
  582.                 !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
  583. }
  584.  
  585. Boolean extract_file(LPCTSTR lpType, LPCTSTR lpName, FSSpec* dst) {
  586.         HGLOBAL datah;
  587.         LPVOID datap;
  588.         HRSRC datarsrc;
  589.         FILECOUNT datalen;
  590.         FILEREF fptr;
  591.         OSErr res;
  592.  
  593.         if ((datarsrc = FindResource((HMODULE)hDllInstance, lpName, lpType))
  594.                 && (datah = LoadResource((HMODULE)hDllInstance, datarsrc))
  595.                 && (datalen = (FILECOUNT)SizeofResource((HMODULE)hDllInstance, datarsrc))
  596.                 && (datap = (Ptr)LockResource(datah))) {
  597.  
  598.                 FSpDelete(dst);
  599.                 if (FSpCreate(dst, kPhotoshopSignature, PS_FILTER_FILETYPE, smUninterp) != noErr) return false;
  600.  
  601.                 if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return false;
  602.  
  603.                 res = FSWrite(fptr, &datalen, datap);
  604.  
  605.                 FSClose(fptr);
  606.  
  607.                 return res == noErr;
  608.         }
  609.         else {
  610.                 return false;
  611.         }
  612. }
  613.  
  614. BOOL StripAuthenticode(FSSpec* dst) {
  615.         HANDLE hFile = CreateFile(&dst->szName[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL);
  616.         if (hFile == INVALID_HANDLE_VALUE) {
  617.                 CloseHandle(hFile);
  618.                 return FALSE;
  619.         }
  620.         if (!_ImageRemoveCertificate(hFile, 0)) {
  621.                 CloseHandle(hFile);
  622.                 return FALSE;
  623.         }
  624.         CloseHandle(hFile);
  625.         return TRUE;
  626. }
  627.  
  628. OSErr do_make_standalone(FSSpec* dst, int bits) {
  629.         Boolean res;
  630.  
  631.         //DeleteFile(dstname);
  632.         if (extract_file(TEXT("TPLT"), MAKEINTRESOURCE(1000 + bits), dst)) {
  633.                 // In case we did digitally sign the FilterFoundry plugin (which is currently not the case though),
  634.                 // we must now remove the signature, because the embedding of parameter data has invalidated it.
  635.                 // Do it before we manipulate anything, in order to avoid that there is an invalid binary (which might annoy AntiVirus software)
  636.                 StripAuthenticode(dst);
  637.  
  638.                 // Now do the resources
  639.                 res = doresources(dst, bits);
  640.                 if (!res) {
  641.                         DeleteFile(&dst->szName[0]);
  642.                         alertuser_id(bits == 32 ? MSG_CANNOT_CREATE_32BIT_FILTER_ID : MSG_CANNOT_CREATE_64BIT_FILTER_ID, (TCHAR*)TEXT("doresources failed"));
  643.                 }
  644.         }
  645.         else {
  646.                 // If you see this error, please make sure that you have called foundry_3264_mixer to include the 32/64 plugins as resource!
  647.                 res = false;
  648.                 //DeleteFile(dstname);
  649.  
  650.                 alertuser_id(bits == 32 ? MSG_CANNOT_CREATE_32BIT_FILTER_ID : MSG_CANNOT_CREATE_64BIT_FILTER_ID, (TCHAR*)TEXT("extract_file failed"));
  651.         }
  652.  
  653.         return res ? noErr : ioErr;
  654. }
  655.  
  656. OSErr make_standalone(StandardFileReply *sfr){
  657.         OSErr tmpErr, outErr;
  658.         FSSpec dst = { 0 };
  659.  
  660.         outErr = noErr;
  661.  
  662.         // Make 32 bit:
  663.         // Destfile = no64_or_32(chosenname)
  664.         xstrcpy(dst.szName, sfr->sfFile.szName);
  665.         remove_64_filename_prefix(&dst.szName[0]);
  666.         tmpErr = do_make_standalone(&dst, 32);
  667.         if (tmpErr != noErr)
  668.                 outErr = tmpErr;
  669.         else
  670.                 showmessage_id(MSG_BUILT32_ID);
  671.  
  672.         // Make 64 bit:
  673.         // Destfile = no64_or_32(chosenname) + 64
  674.         xstrcpy(dst.szName, sfr->sfFile.szName);
  675.         remove_64_filename_prefix(&dst.szName[0]);
  676.         add_64_filename_prefix(&dst.szName[0]);
  677.         tmpErr = do_make_standalone(&dst, 64);
  678.         if (tmpErr != noErr)
  679.                 outErr = tmpErr;
  680.         else
  681.                 showmessage_id(MSG_BUILT64_ID);
  682.  
  683.         return outErr;
  684. }
  685.  
  686.