Subversion Repositories filter_foundry

Rev

Rev 554 | 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-2023 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 "file_compat.h"
  24.  
  25. #include <string.h>
  26.  
  27. static BOOL CALLBACK enum_find_resname(HMODULE hModule, LPCTSTR lpszType,
  28.         LPTSTR lpszName, LONG_PTR lParam)
  29. {
  30.         UNREFERENCED_PARAMETER(hModule);
  31.         UNREFERENCED_PARAMETER(lpszType);
  32.  
  33.         if (IS_INTRESOURCE(lpszName)) {
  34.                 UINT16* pparm_id = (UINT16*)lParam;
  35.                 *pparm_id = (UINT16)((intptr_t)lpszName & 0xFFFF);
  36.                 return false; // we only want the first one
  37.         }
  38.         else return true;
  39. }
  40.  
  41. FFLoadingResult readPARMresource(HMODULE hm) {
  42.         HRSRC resinfo;
  43.         HANDLE h;
  44.         Ptr pparm;
  45.  
  46.         UINT16 parm_id = 1;
  47.         EnumResourceNames(hm, PARM_TYPE, enum_find_resname, (LONG_PTR)&parm_id); // callback function enum_find_resname() will find the actual found parm_id
  48.  
  49.         // load first PARM resource
  50.         if ((resinfo = FindResource(hm, MAKEINTRESOURCE(parm_id), PARM_TYPE))) {
  51.                 if ((h = LoadResource(hm, resinfo)) && (pparm = (Ptr)LockResource(h))) {
  52.                         FFLoadingResult res = readPARM(&gdata->parm, pparm);
  53.                         gdata->obfusc = false;
  54.                         return res;
  55.                 }
  56.         }
  57.         else if (
  58.                 ((resinfo = FindResource(hm, OBFUSCDATA_ID_NEW, OBFUSCDATA_TYPE_NEW)) ||
  59.                         (resinfo = FindResource(hm, OBFUSCDATA_ID_OLD, OBFUSCDATA_TYPE_OLD)))) {
  60.                 if ((h = LoadResource(hm, resinfo)) && (pparm = (Ptr)LockResource(h))) {
  61.                         // Fix by DM, 18 Dec 2018:
  62.                         // We need to copy the information, because the resource data is read-only
  63.                         DWORD resSize = SizeofResource(hm, resinfo);
  64.                         if (resSize == sizeof(PARM_T)) {
  65.                                 FFLoadingResult res;
  66.                                 PARM_T* copy = (PARM_T*)malloc(resSize);
  67.                                 if (!copy) return MSG_OUT_OF_MEMORY_ID;
  68.                                 memcpy(copy, pparm, resSize);
  69.                                 deobfusc(copy);
  70.                                 res = readPARM(&gdata->parm, (Ptr)copy);
  71.                                 free(copy);
  72.                                 gdata->obfusc = true;
  73.                                 return res;
  74.                         }
  75.                         else {
  76.                                 // Obfuscationed PARM has wrong size. It is probably a file with different RCDATA
  77.                                 gdata->obfusc = false;
  78.                                 return MSG_INVALID_PARAMETER_DATA_ID;
  79.                         }
  80.                 }
  81.         }
  82.         return MSG_LOADFILE_UNKNOWN_FORMAT_ID;
  83. }
  84.  
  85. FFLoadingResult loadfile(StandardFileReply* sfr) {
  86.         HMODULE hm;
  87.         FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID;
  88.  
  89.         // First, try to read the file as AFS/PFF/TXT file
  90.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  91.                 if (LOADING_OK == (res = readfile_afs_pff(sfr))) {
  92.                         gdata->obfusc = false;
  93.                         parm_reset(true, false, true, false);
  94.                         return 0;
  95.                 }
  96.                 if (!fileHasExtension(sfr, TEXT(".afs")) && !fileHasExtension(sfr, TEXT(".pff"))) {
  97.                         // If .afs and .pff files have an invalid signature, then it is a hard error.
  98.                         // If any other file has no "%RGB1.0" signature, then it is OK and
  99.                         // we will return MSG_LOADFILE_UNKNOWN_FORMAT_ID and continue with trying other formats
  100.                         if (res == MSG_INVALID_FILE_SIGNATURE_ID) res = MSG_LOADFILE_UNKNOWN_FORMAT_ID;
  101.                 }
  102.         }
  103.  
  104.         // Try to read the file as FFL file
  105.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  106.                 if (LOADING_OK == (res = readfile_ffl(sfr))) {
  107.                         gdata->obfusc = false;
  108.                         parm_reset(true, true, true, true);
  109.                         return 0;
  110.                 }
  111.                 if (!fileHasExtension(sfr, TEXT(".ffl"))) {
  112.                         // If .ffl files have an invalid signature, then it is a hard error.
  113.                         // If any other file has no "FFL1.0" signature, then it is OK and
  114.                         // we will return MSG_LOADFILE_UNKNOWN_FORMAT_ID and continue with trying other formats
  115.                         if (res == MSG_INVALID_FILE_SIGNATURE_ID) res = MSG_LOADFILE_UNKNOWN_FORMAT_ID;
  116.                 }
  117.         }
  118.  
  119.         // Is it a "Filters Unlimited" FFX filter? (Only partially compatible with Filter Factory!!!)
  120.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  121.                 if (LOADING_OK == (res = readfile_ffx(sfr))) {
  122.                         return 0;
  123.                 }
  124.                 if (!fileHasExtension(sfr, TEXT(".ffx"))) {
  125.                         // If .ffx files have an invalid signature, then it is a hard error.
  126.                         // If any other file has no "FFX1.0", "FFX1.1", or "FFX1.2" signature, then it is OK and
  127.                         // we will return MSG_LOADFILE_UNKNOWN_FORMAT_ID and continue with trying other formats
  128.                         if (res == MSG_INVALID_FILE_SIGNATURE_ID) res = MSG_LOADFILE_UNKNOWN_FORMAT_ID;
  129.                 }
  130.         }
  131.  
  132.         // If that didn't work, try to load as Windows image file (Resource API for 8BF/PRM files)
  133.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  134.                 if (hm = LoadLibraryEx(sfr->sfFile.szName, NULL, LOAD_LIBRARY_AS_DATAFILE)) {
  135.                         if (LOADING_OK == (res = readPARMresource(hm))) {
  136.                                 gdata->parm.standalone = false; // just because the loaded file is standalone, does not mean that WE are standalone
  137.                                 if (gdata->parm.iProtected) {
  138.                                         parm_reset(true, true, true, true);
  139.                                         res = MSG_FILTER_PROTECTED_ID;
  140.                                 }
  141.                                 else {
  142.                                         FreeLibrary(hm);
  143.                                         return 0;
  144.                                 }
  145.                         }
  146.                         FreeLibrary(hm);
  147.                 }
  148.         }
  149.  
  150.         // Is it a "Filters Unlimited" TXT filter? (Only partially compatible with Filter Factory!!!)
  151.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  152.                 if (fileHasExtension(sfr, TEXT(".txt"))) {
  153.                         if (LOADING_OK == (res = readfile_picotxt_or_ffdecomp(sfr))) {
  154.                                 return 0;
  155.                         }
  156.                 }
  157.         }
  158.  
  159.         // Is it a "GIMP UserFilter (GUF)" file? (Only partially compatible with Filter Factory!!!)
  160.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  161.                 if (fileHasExtension(sfr, TEXT(".guf"))) {
  162.                         if (LOADING_OK == (res = readfile_guf(sfr))) {
  163.                                 return 0;
  164.                         }
  165.                 }
  166.         }
  167.  
  168.         // If nothing worked, we will try to find a PARM resource (MacOS plugin, or 64 bit 8BF on Win32 OS)
  169.         // Note that we cannot detect obfuscated filters here!
  170.         if (res == MSG_LOADFILE_UNKNOWN_FORMAT_ID) {
  171.                 if (LOADING_OK == (res = readfile_8bf(sfr))) {
  172.                         if (gdata->parm.iProtected) {
  173.                                 // This is for purely protected filters before the time when obfuscation and protection was merged
  174.                                 parm_reset(true, true, true, true);
  175.                                 res = MSG_FILTER_PROTECTED_ID;
  176.                         }
  177.                         else {
  178.                                 return 0;
  179.                         }
  180.                 }
  181.         }
  182.  
  183.         return res;
  184. }
  185.