Rev 498 | Rev 537 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
256 | daniel-mar | 1 | /* |
408 | daniel-mar | 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 |
||
503 | daniel-mar | 4 | Copyright (C) 2018-2022 Daniel Marschall, ViaThinkSoft |
256 | daniel-mar | 5 | |
408 | daniel-mar | 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. |
||
256 | daniel-mar | 10 | |
408 | daniel-mar | 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. |
||
256 | daniel-mar | 15 | |
408 | daniel-mar | 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 |
||
256 | daniel-mar | 19 | */ |
20 | |||
21 | #include "ff.h" |
||
22 | |||
23 | #include "file_compat.h" |
||
24 | |||
25 | #include <string.h> |
||
26 | |||
436 | daniel-mar | 27 | static BOOL CALLBACK enum_find_resname(HMODULE hModule, LPCTSTR lpszType, |
408 | daniel-mar | 28 | LPTSTR lpszName, LONG_PTR lParam) |
256 | daniel-mar | 29 | { |
433 | daniel-mar | 30 | UNREFERENCED_PARAMETER(hModule); |
31 | UNREFERENCED_PARAMETER(lpszType); |
||
32 | |||
345 | daniel-mar | 33 | if (IS_INTRESOURCE(lpszName)) { |
436 | daniel-mar | 34 | UINT16* pparm_id = (UINT16*)lParam; |
35 | *pparm_id = (UINT16)((intptr_t)lpszName & 0xFFFF); |
||
345 | daniel-mar | 36 | return false; // we only want the first one |
37 | } |
||
38 | else return true; |
||
256 | daniel-mar | 39 | } |
40 | |||
492 | daniel-mar | 41 | Boolean readPARMresource(HMODULE hm, TCHAR** reason) { |
256 | daniel-mar | 42 | HRSRC resinfo; |
43 | HANDLE h; |
||
44 | Ptr pparm; |
||
45 | |||
436 | daniel-mar | 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 |
||
256 | daniel-mar | 48 | |
49 | // load first PARM resource |
||
408 | daniel-mar | 50 | if ((resinfo = FindResource(hm, MAKEINTRESOURCE(parm_id), PARM_TYPE))) { |
256 | daniel-mar | 51 | if ((h = LoadResource(hm, resinfo)) && (pparm = (Ptr)LockResource(h))) { |
456 | daniel-mar | 52 | Boolean res = readPARM(&gdata->parm, pparm); |
256 | daniel-mar | 53 | gdata->obfusc = false; |
408 | daniel-mar | 54 | return res; |
256 | daniel-mar | 55 | } |
408 | daniel-mar | 56 | } |
440 | daniel-mar | 57 | else if ( |
408 | daniel-mar | 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))) { |
||
256 | daniel-mar | 61 | // Fix by DM, 18 Dec 2018: |
62 | // We need to copy the information, because the resource data is read-only |
||
408 | daniel-mar | 63 | DWORD resSize = SizeofResource(hm, resinfo); |
271 | daniel-mar | 64 | if (resSize == sizeof(PARM_T)) { |
456 | daniel-mar | 65 | Boolean res; |
271 | daniel-mar | 66 | PARM_T* copy = (PARM_T*)malloc(resSize); |
67 | if (!copy) return false; |
||
68 | memcpy(copy, pparm, resSize); |
||
69 | deobfusc(copy); |
||
408 | daniel-mar | 70 | res = readPARM(&gdata->parm, (Ptr)copy); |
71 | if (!res) { |
||
498 | daniel-mar | 72 | if (reason) *reason = FF_GetMsg_Cpy(MSG_INCOMPATIBLE_OBFUSCATION_ID); |
408 | daniel-mar | 73 | } |
74 | free(copy); |
||
271 | daniel-mar | 75 | gdata->obfusc = true; |
408 | daniel-mar | 76 | return res; |
77 | } |
||
78 | else { |
||
309 | daniel-mar | 79 | // Obfuscationed PARM has wrong size. It is probably a file with different RCDATA |
271 | daniel-mar | 80 | gdata->obfusc = false; |
81 | return false; |
||
82 | } |
||
256 | daniel-mar | 83 | } |
84 | } |
||
408 | daniel-mar | 85 | return false; |
256 | daniel-mar | 86 | } |
87 | |||
492 | daniel-mar | 88 | Boolean loadfile(StandardFileReply* sfr, TCHAR** reason) { |
256 | daniel-mar | 89 | HMODULE hm; |
498 | daniel-mar | 90 | TCHAR* reasonstr; |
256 | daniel-mar | 91 | |
408 | daniel-mar | 92 | // The different read-functions will return true if the resource was successfully loaded, |
93 | // or false otherwise. If *reason is set, then the answer is clearly "No". If the result |
||
94 | // is just false, it means that the program should continue with the next read-function. |
||
498 | daniel-mar | 95 | reasonstr = NULL; |
408 | daniel-mar | 96 | |
256 | daniel-mar | 97 | // First, try to read the file as AFS/PFF/TXT file |
498 | daniel-mar | 98 | if (reasonstr == NULL) { |
99 | if (readfile_afs_pff(sfr, &reasonstr)) { |
||
408 | daniel-mar | 100 | gdata->obfusc = false; |
101 | gdata->parmloaded = false; |
||
102 | return true; |
||
103 | } |
||
256 | daniel-mar | 104 | } |
105 | |||
106 | // If that didn't work, try to load as Windows image file (Resource API for 8BF/PRM files) |
||
498 | daniel-mar | 107 | if (reasonstr == NULL) { |
444 | daniel-mar | 108 | if (hm = LoadLibraryEx(sfr->sfFile.szName, NULL, LOAD_LIBRARY_AS_DATAFILE)) { |
498 | daniel-mar | 109 | if (readPARMresource(hm, &reasonstr)) { |
408 | daniel-mar | 110 | if (gdata->parm.iProtected) { |
498 | daniel-mar | 111 | reasonstr = FF_GetMsg_Cpy(MSG_FILTER_PROTECTED_ID); |
292 | daniel-mar | 112 | //gdata->parmloaded = false; |
256 | daniel-mar | 113 | } |
408 | daniel-mar | 114 | else { |
115 | gdata->parmloaded = true; |
||
116 | FreeLibrary(hm); |
||
117 | return true; |
||
118 | } |
||
256 | daniel-mar | 119 | } |
120 | FreeLibrary(hm); |
||
121 | } |
||
122 | } |
||
123 | |||
366 | daniel-mar | 124 | // Is it a "Filters Unlimited" filter? (Only partially compatible with Filter Factory!!!) |
498 | daniel-mar | 125 | if (reasonstr == NULL) { |
126 | if (readfile_ffx(sfr, &reasonstr)) { |
||
408 | daniel-mar | 127 | gdata->parmloaded = true; |
128 | return true; |
||
366 | daniel-mar | 129 | } |
130 | } |
||
131 | |||
385 | daniel-mar | 132 | // Is it a "Filters Unlimited" filter? (Only partially compatible with Filter Factory!!!) |
498 | daniel-mar | 133 | if (reasonstr == NULL) { |
134 | if (readfile_picotxt(sfr, &reasonstr)) { |
||
408 | daniel-mar | 135 | gdata->parmloaded = true; |
136 | return true; |
||
385 | daniel-mar | 137 | } |
138 | } |
||
139 | |||
408 | daniel-mar | 140 | // If nothing worked, we will try to find a PARM resource (MacOS plugin, or 64 bit 8BF on Win32 OS) |
141 | // Note that we cannot detect obfuscated filters here! |
||
498 | daniel-mar | 142 | if (reasonstr == NULL) { |
143 | if (readfile_8bf(sfr, &reasonstr)) { |
||
256 | daniel-mar | 144 | if (gdata->parm.iProtected) { |
408 | daniel-mar | 145 | // This is for purely protected filters before the time when obfuscation and protection was merged |
498 | daniel-mar | 146 | reasonstr = FF_GetMsg_Cpy(MSG_FILTER_PROTECTED_ID); |
256 | daniel-mar | 147 | } |
148 | else { |
||
408 | daniel-mar | 149 | gdata->parmloaded = true; |
150 | return true; |
||
256 | daniel-mar | 151 | } |
152 | } |
||
153 | } |
||
154 | |||
408 | daniel-mar | 155 | // We didn't had success. If we have a clear reason, return false and the reason. |
156 | // If we don't have a clear reason, set a generic reason and return false. |
||
498 | daniel-mar | 157 | if (reasonstr == NULL) { |
158 | reasonstr = FF_GetMsg_Cpy(MSG_LOADFILE_UNKNOWN_FORMAT_ID); |
||
256 | daniel-mar | 159 | } |
498 | daniel-mar | 160 | |
161 | if (reason) *reason = reasonstr; |
||
162 | |||
408 | daniel-mar | 163 | return false; |
422 | daniel-mar | 164 | } |