Subversion Repositories filter_foundry

Rev

Rev 422 | Rev 436 | 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. #include "ff.h"
  22.  
  23. #include "file_compat.h"
  24.  
  25. #include <string.h>
  26.  
  27. static UINT16 parm_id;
  28.  
  29. static BOOL CALLBACK enumnames(HMODULE hModule, LPCTSTR lpszType,
  30.         LPTSTR lpszName, LONG_PTR lParam)
  31. {
  32.         UNREFERENCED_PARAMETER(lParam); // TODO: Pass parm_id as pointer in lParam
  33.         UNREFERENCED_PARAMETER(hModule);
  34.         UNREFERENCED_PARAMETER(lpszType);
  35.  
  36.         if (IS_INTRESOURCE(lpszName)) {
  37.                 parm_id = (UINT16)((intptr_t)lpszName & 0xFFFF);
  38.                 return false; // we only want the first one
  39.         }
  40.         else return true;
  41. }
  42.  
  43. Boolean readPARMresource(HMODULE hm, char** reason, int readobfusc) {
  44.         HRSRC resinfo;
  45.         HANDLE h;
  46.         Ptr pparm;
  47.  
  48.         parm_id = 1;
  49.         EnumResourceNames(hm, PARM_TYPE, enumnames, 0); // callback function enumnames() will find the actual found parm_id
  50.  
  51.         // load first PARM resource
  52.         if ((resinfo = FindResource(hm, MAKEINTRESOURCE(parm_id), PARM_TYPE))) {
  53.                 if ((h = LoadResource(hm, resinfo)) && (pparm = (Ptr)LockResource(h))) {
  54.                         int res = readPARM(&gdata->parm, pparm);
  55.                         gdata->obfusc = false;
  56.                         return res;
  57.                 }
  58.         }
  59.         else if (readobfusc &&
  60.                 ((resinfo = FindResource(hm, OBFUSCDATA_ID_NEW, OBFUSCDATA_TYPE_NEW)) ||
  61.                         (resinfo = FindResource(hm, OBFUSCDATA_ID_OLD, OBFUSCDATA_TYPE_OLD)))) {
  62.                 if ((h = LoadResource(hm, resinfo)) && (pparm = (Ptr)LockResource(h))) {
  63.                         // Fix by DM, 18 Dec 2018:
  64.                         // We need to copy the information, because the resource data is read-only
  65.                         DWORD resSize = SizeofResource(hm, resinfo);
  66.                         if (resSize == sizeof(PARM_T)) {
  67.                                 int res;
  68.                                 PARM_T* copy = (PARM_T*)malloc(resSize);
  69.                                 if (!copy) return false;
  70.                                 memcpy(copy, pparm, resSize);
  71.                                 deobfusc(copy);
  72.                                 res = readPARM(&gdata->parm, (Ptr)copy);
  73.                                 if (!res) {
  74.                                         *reason = _strdup("Incompatible obfuscation.");
  75.                                 }
  76.                                 free(copy);
  77.                                 gdata->obfusc = true;
  78.                                 return res;
  79.                         }
  80.                         else {
  81.                                 // Obfuscationed PARM has wrong size. It is probably a file with different RCDATA
  82.                                 gdata->obfusc = false;
  83.                                 return false;
  84.                         }
  85.                 }
  86.         }
  87.         return false;
  88. }
  89.  
  90. Boolean loadfile(StandardFileReply* sfr, char** reason) {
  91.         HMODULE hm;
  92.  
  93.         // The different read-functions will return true if the resource was successfully loaded,
  94.         // or false otherwise. If *reason is set, then the answer is clearly "No". If the result
  95.         // is just false, it means that the program should continue with the next read-function.
  96.         *reason = NULL;
  97.  
  98.         // First, try to read the file as AFS/PFF/TXT file
  99.         if (*reason == NULL) {
  100.                 if (readfile_afs_pff(sfr, reason)) {
  101.                         gdata->obfusc = false;
  102.                         gdata->parmloaded = false;
  103.                         return true;
  104.                 }
  105.         }
  106.  
  107.         // If that didn't work, try to load as Windows image file (Resource API for 8BF/PRM files)
  108.         if (*reason == NULL) {
  109.                 char name[MAX_PATH + 1];
  110.                 if (hm = LoadLibraryEx(myp2cstrcpy(name, sfr->sfFile.name), NULL, LOAD_LIBRARY_AS_DATAFILE)) {
  111.                         if (readPARMresource(hm, reason, READ_OBFUSC)) {
  112.                                 if (gdata->parm.iProtected) {
  113.                                         *reason = _strdup("The filter is protected.");
  114.                                         //gdata->parmloaded = false;
  115.                                 }
  116.                                 else {
  117.                                         gdata->parmloaded = true;
  118.                                         FreeLibrary(hm);
  119.                                         return true;
  120.                                 }
  121.                         }
  122.                         FreeLibrary(hm);
  123.                 }
  124.         }
  125.  
  126.         // Is it a "Filters Unlimited" filter? (Only partially compatible with Filter Factory!!!)
  127.         if (*reason == NULL) {
  128.                 if (readfile_ffx(sfr, reason)) {
  129.                         gdata->parmloaded = true;
  130.                         return true;
  131.                 }
  132.         }
  133.  
  134.         // Is it a "Filters Unlimited" filter? (Only partially compatible with Filter Factory!!!)
  135.         if (*reason == NULL) {
  136.                 if (readfile_picotxt(sfr, reason)) {
  137.                         gdata->parmloaded = true;
  138.                         return true;
  139.                 }
  140.         }
  141.  
  142.         // If nothing worked, we will try to find a PARM resource (MacOS plugin, or 64 bit 8BF on Win32 OS)
  143.         // Note that we cannot detect obfuscated filters here!
  144.         if (*reason == NULL) {
  145.                 if (readfile_8bf(sfr, reason)) {
  146.                         if (gdata->parm.iProtected) {
  147.                                 // This is for purely protected filters before the time when obfuscation and protection was merged
  148.                                 *reason = _strdup("The filter is protected.");
  149.                         }
  150.                         else {
  151.                                 gdata->parmloaded = true;
  152.                                 return true;
  153.                         }
  154.                 }
  155.         }
  156.  
  157.         // We didn't had success. If we have a clear reason, return false and the reason.
  158.         // If we don't have a clear reason, set a generic reason and return false.
  159.         if (*reason == NULL) {
  160.                 *reason = _strdup("It is not a text parameter file, nor a standalone Mac/PC filter created by Filter Factory/Filter Foundry.");
  161.         }
  162.         return false;
  163. }
  164.