Subversion Repositories filter_foundry

Rev

Rev 250 | Rev 256 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 toby 1
/*
18 toby 2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
192 daniel-mar 3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
206 daniel-mar 4
    Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
2 toby 5
 
6
    This program is free software; you can redistribute it and/or modify
106 dmarschall 7
    it under the terms of the GNU General Public License as published by
2 toby 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
 
106 dmarschall 16
    You should have received a copy of the GNU General Public License
2 toby 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
 
116 dmarschall 27
static UINT16 parm_id;
35 toby 28
 
62 toby 29
static BOOL CALLBACK enumnames(HMODULE hModule,LPCTSTR lpszType,
237 daniel-mar 30
                               LPTSTR lpszName,LONG_PTR lParam)
62 toby 31
{
35 toby 32
        if(IS_INTRESOURCE(lpszName))
116 dmarschall 33
                parm_id = (UINT16)((intptr_t)lpszName & 0xFFFF);
35 toby 34
        return false; // we only want the first one
35
}
36
 
87 toby 37
Boolean readPARMresource(HMODULE hm,char **reason,int readobfusc){
2 toby 38
        HRSRC resinfo;
39
        HANDLE h;
40
        Ptr pparm;
87 toby 41
        int res = false;
106 dmarschall 42
 
35 toby 43
        parm_id = PARM_ID;
44
        EnumResourceNames(hm,"PARM",enumnames,0);
106 dmarschall 45
 
35 toby 46
        // load first PARM resource
87 toby 47
        if( (resinfo = FindResource(hm,MAKEINTRESOURCE(parm_id),"PARM")) ){
198 daniel-mar 48
                if( (h = LoadResource(hm,resinfo)) && (pparm = (Ptr)LockResource(h)) )
87 toby 49
                        res = readPARM(pparm,&gdata->parm,reason,1 /*Windows format resource*/);
50
        }else if( readobfusc && (resinfo = FindResource(hm,MAKEINTRESOURCE(OBFUSCDATA_ID),RT_RCDATA)) ){
198 daniel-mar 51
                if( (h = LoadResource(hm,resinfo)) && (pparm = (Ptr)LockResource(h)) ){
106 dmarschall 52
                        // Fix by DM, 18 Dec 2018:
53
                        // We need to copy the information, because the resource data is read-only
54
                        DWORD resSize = SizeofResource(hm,resinfo);
194 daniel-mar 55
                        char* copy = (char*)malloc(resSize);
211 daniel-mar 56
                        if (!copy) return false;
106 dmarschall 57
                        memcpy(copy, pparm, resSize);
255 daniel-mar 58
                        deobfusc((unsigned char*)copy, resSize,OBFUSC_SEED_POS);
106 dmarschall 59
                        res = readPARM(copy,&gdata->parm,reason,1);
60
                        free(copy);
85 toby 61
                }
62
        }
87 toby 63
        return res;
2 toby 64
}
65
 
66
Boolean loadfile(StandardFileReply *sfr,char **reason){
67
        Boolean readok = false;
68
        HMODULE hm;
69
 
153 dmarschall 70
        // First, try to read the file as AFS/PFF/TXT file
194 daniel-mar 71
        if( (readok = readfile(sfr,reason)) ){
153 dmarschall 72
                gdata->parmloaded = false;
73
        }
147 dmarschall 74
 
153 dmarschall 75
        // If that didn't work, try to load as Windows image file (Resource API for 8BF/PRM files)
76
        if (!readok) {
188 dmarschall 77
                char name[MAX_PATH+1];
78
                if (hm = LoadLibraryEx(myp2cstrcpy(name,sfr->sfFile.name),NULL,LOAD_LIBRARY_AS_DATAFILE)) {
249 daniel-mar 79
                        if (readPARMresource(hm,reason,READ_OBFUSC)) {
153 dmarschall 80
                                if (gdata->parm.iProtected) {
198 daniel-mar 81
                                        *reason = _strdup("The filter is protected.");
250 daniel-mar 82
                                        return false; // Stop! We know the issue now.
152 dmarschall 83
                                } else {
153 dmarschall 84
                                        readok = gdata->parmloaded = true;
147 dmarschall 85
                                }
2 toby 86
                        }
153 dmarschall 87
                        FreeLibrary(hm);
2 toby 88
                }
89
        }
90
 
153 dmarschall 91
        // If nothing worked, we will try to find a PARM resource (MacOS plugin, or NE executable on Win64)
92
        if (!readok) {
250 daniel-mar 93
                if (read8bfplugin(sfr, reason)) {
94
                        if (gdata->parm.iProtected) {
95
                                *reason = _strdup("The filter is protected.");
96
                                return false; // Stop! We know the issue now.
97
                        }
98
                        else {
99
                                readok = gdata->parmloaded = true;
100
                        }
153 dmarschall 101
                }
102
        }
103
 
104
        // Check if we had success
105
        if (!readok) {
250 daniel-mar 106
                *reason = _strdup("It is not a text parameter (AFS) file, nor a standalone Mac/PC filter made by Filter Factory/Filter Foundry.");
153 dmarschall 107
        }
108
 
2 toby 109
        return readok;
110
}