Rev 345 | Rev 373 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
256 | daniel-mar | 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 | { |
||
345 | daniel-mar | 32 | if (IS_INTRESOURCE(lpszName)) { |
256 | daniel-mar | 33 | parm_id = (UINT16)((intptr_t)lpszName & 0xFFFF); |
345 | daniel-mar | 34 | return false; // we only want the first one |
35 | } |
||
36 | else return true; |
||
256 | daniel-mar | 37 | } |
38 | |||
39 | Boolean readPARMresource(HMODULE hm,char **reason,int readobfusc){ |
||
40 | HRSRC resinfo; |
||
41 | HANDLE h; |
||
42 | Ptr pparm; |
||
43 | int res = false; |
||
44 | |||
345 | daniel-mar | 45 | parm_id = PARM_ID; // default value |
46 | EnumResourceNames(hm,"PARM",enumnames,0); // callback function enumnames() will find the actual found parm_id |
||
256 | daniel-mar | 47 | |
48 | // load first PARM resource |
||
49 | if( (resinfo = FindResource(hm,MAKEINTRESOURCE(parm_id),"PARM")) ){ |
||
50 | if ((h = LoadResource(hm, resinfo)) && (pparm = (Ptr)LockResource(h))) { |
||
51 | res = readPARM(pparm, &gdata->parm, reason, 1 /*Windows format resource*/); |
||
52 | gdata->obfusc = false; |
||
53 | } |
||
54 | }else if( readobfusc && (resinfo = FindResource(hm,MAKEINTRESOURCE(OBFUSCDATA_ID),RT_RCDATA)) ){ |
||
55 | if( (h = LoadResource(hm,resinfo)) && (pparm = (Ptr)LockResource(h)) ){ |
||
56 | // Fix by DM, 18 Dec 2018: |
||
57 | // We need to copy the information, because the resource data is read-only |
||
58 | DWORD resSize = SizeofResource(hm,resinfo); |
||
271 | daniel-mar | 59 | if (resSize == sizeof(PARM_T)) { |
60 | PARM_T* copy = (PARM_T*)malloc(resSize); |
||
61 | if (!copy) return false; |
||
62 | memcpy(copy, pparm, resSize); |
||
63 | deobfusc(copy); |
||
64 | res = readPARM((Ptr)copy,&gdata->parm,reason,1); |
||
65 | gdata->obfusc = true; |
||
66 | free(copy); |
||
67 | } else { |
||
309 | daniel-mar | 68 | // Obfuscationed PARM has wrong size. It is probably a file with different RCDATA |
271 | daniel-mar | 69 | gdata->obfusc = false; |
70 | return false; |
||
71 | } |
||
256 | daniel-mar | 72 | } |
73 | } |
||
74 | if (!res) { |
||
75 | gdata->obfusc = false; |
||
76 | } |
||
77 | return res; |
||
78 | } |
||
79 | |||
80 | Boolean loadfile(StandardFileReply *sfr,char **reason){ |
||
81 | Boolean readok = false; |
||
82 | HMODULE hm; |
||
83 | |||
84 | // First, try to read the file as AFS/PFF/TXT file |
||
366 | daniel-mar | 85 | if( (readok = readfile_afs_pff(sfr,reason)) ){ |
256 | daniel-mar | 86 | gdata->obfusc = false; |
87 | gdata->parmloaded = false; |
||
88 | } |
||
89 | |||
90 | // If that didn't work, try to load as Windows image file (Resource API for 8BF/PRM files) |
||
91 | if (!readok) { |
||
92 | char name[MAX_PATH+1]; |
||
93 | if (hm = LoadLibraryEx(myp2cstrcpy(name,sfr->sfFile.name),NULL,LOAD_LIBRARY_AS_DATAFILE)) { |
||
94 | if (readPARMresource(hm,reason,READ_OBFUSC)) { |
||
95 | if ((gdata->parm.cbSize != PARM_SIZE) && (gdata->parm.cbSize != PARM_SIZE_PREMIERE) && (gdata->parm.cbSize != PARM_SIG_MAC)) { |
||
292 | daniel-mar | 96 | *reason = _strdup("Incompatible obfuscation."); |
97 | //gdata->parmloaded = false; |
||
256 | daniel-mar | 98 | return false; // Stop! We know the issue now. |
99 | } else if (gdata->parm.iProtected) { |
||
100 | *reason = _strdup("The filter is protected."); |
||
292 | daniel-mar | 101 | //gdata->parmloaded = false; |
256 | daniel-mar | 102 | return false; // Stop! We know the issue now. |
103 | } else { |
||
104 | readok = gdata->parmloaded = true; |
||
105 | } |
||
106 | } |
||
107 | FreeLibrary(hm); |
||
108 | } |
||
109 | } |
||
110 | |||
366 | daniel-mar | 111 | // Is it a "Filters Unlimited" filter? (Only partially compatible with Filter Factory!!!) |
112 | if (!readok) { |
||
113 | if (readfile_ffx(sfr, reason)) { |
||
114 | readok = gdata->parmloaded = true; |
||
115 | } |
||
116 | } |
||
117 | |||
256 | daniel-mar | 118 | // If nothing worked, we will try to find a PARM resource (MacOS plugin, or NE executable on Win64) |
119 | if (!readok) { |
||
120 | if (read8bfplugin(sfr, reason)) { |
||
121 | if (gdata->parm.iProtected) { |
||
122 | *reason = _strdup("The filter is protected."); |
||
123 | return false; // Stop! We know the issue now. |
||
124 | } |
||
125 | else { |
||
126 | readok = gdata->parmloaded = true; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // Check if we had success |
||
132 | if (!readok) { |
||
133 | *reason = _strdup("It is not a text parameter (AFS) file, nor a standalone Mac/PC filter made by Filter Factory/Filter Foundry."); |
||
134 | } |
||
135 | |||
136 | return readok; |
||
137 | } |