Subversion Repositories filter_foundry

Rev

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