Subversion Repositories filter_foundry

Rev

Rev 189 | Rev 194 | 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-2019 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. // see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/introductiontoresources/resourcereference/resourcefunctions/findresource.asp
  30. static BOOL CALLBACK enumnames(HMODULE hModule,LPCTSTR lpszType,
  31.                                                            LPTSTR lpszName,LONG_PTR lParam)
  32. {
  33.         if(IS_INTRESOURCE(lpszName))
  34.                 parm_id = (UINT16)((intptr_t)lpszName & 0xFFFF);
  35.         return false; // we only want the first one
  36. }
  37.  
  38. Boolean readPARMresource(HMODULE hm,char **reason,int readobfusc){
  39.         HRSRC resinfo;
  40.         HANDLE h;
  41.         Ptr pparm;
  42.         int res = false;
  43.  
  44.         parm_id = PARM_ID;
  45.         EnumResourceNames(hm,"PARM",enumnames,0);
  46.  
  47.         // load first PARM resource
  48.         if( (resinfo = FindResource(hm,MAKEINTRESOURCE(parm_id),"PARM")) ){
  49.                 if( (h = LoadResource(hm,resinfo)) && (pparm = LockResource(h)) )
  50.                         res = readPARM(pparm,&gdata->parm,reason,1 /*Windows format resource*/);
  51.         }else if( readobfusc && (resinfo = FindResource(hm,MAKEINTRESOURCE(OBFUSCDATA_ID),RT_RCDATA)) ){
  52.                 if( (h = LoadResource(hm,resinfo)) && (pparm = LockResource(h)) ){
  53.                         // Fix by DM, 18 Dec 2018:
  54.                         // We need to copy the information, because the resource data is read-only
  55.                         DWORD resSize = SizeofResource(hm,resinfo);
  56.                         char* copy = malloc(resSize);
  57.                         memcpy(copy, pparm, resSize);
  58.                         obfusc((unsigned char*)copy, resSize);
  59.                         res = readPARM(copy,&gdata->parm,reason,1);
  60.                         free(copy);
  61.                 }
  62.         }
  63.         return res;
  64. }
  65.  
  66. Boolean loadfile(StandardFileReply *sfr,char **reason){
  67.         Boolean readok = false;
  68.         HMODULE hm;
  69.  
  70.         // First, try to read the file as AFS/PFF/TXT file
  71.         if (readok = readfile(sfr,reason)) {
  72.                 gdata->parmloaded = false;
  73.         }
  74.  
  75.         // If that didn't work, try to load as Windows image file (Resource API for 8BF/PRM files)
  76.         if (!readok) {
  77.                 // see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibraryex.asp
  78.                 char name[MAX_PATH+1];
  79.                 if (hm = LoadLibraryEx(myp2cstrcpy(name,sfr->sfFile.name),NULL,LOAD_LIBRARY_AS_DATAFILE)) {
  80.                         if (readPARMresource(hm,reason,0)) {
  81.                                 if (gdata->parm.iProtected) {
  82.                                         *reason = "The filter is protected.";
  83.                                 } else {
  84.                                         readok = gdata->parmloaded = true;
  85.                                 }
  86.                         } else {
  87.                                 *reason = "It is not a standalone filter made by Filter Factory/Filter Foundry.";
  88.                         }
  89.                         FreeLibrary(hm);
  90.                 }
  91.         }
  92.  
  93.         // If nothing worked, we will try to find a PARM resource (MacOS plugin, or NE executable on Win64)
  94.         if (!readok) {
  95.                 if (readok = read8bfplugin(sfr, reason)) {
  96.                         gdata->parmloaded = true;
  97.                 } else {
  98.                         *reason = "PARM resource was not found in this plugin file, or this is not a standalone plugin.";
  99.                 }
  100.         }
  101.  
  102.         // Check if we had success
  103.         if (!readok) {
  104.                 *reason = "PARM resource was not found in this plugin file, or this is not a standalone plugin.";
  105.         }
  106.  
  107.         return readok;
  108. }
  109.