Subversion Repositories filter_foundry

Rev

Rev 145 | Rev 149 | 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
85 toby 3
    Copyright (C) 2003-7 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
106 dmarschall 6
    it under the terms of the GNU General Public License as published by
2 toby 7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
106 dmarschall 15
    You should have received a copy of the GNU General Public License
2 toby 16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
 
20
#include "ff.h"
21
 
22
#include "file_compat.h"
23
 
24
#include <string.h>
25
 
116 dmarschall 26
static UINT16 parm_id;
35 toby 27
 
28
// see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/introductiontoresources/resourcereference/resourcefunctions/findresource.asp
62 toby 29
static BOOL CALLBACK enumnames(HMODULE hModule,LPCTSTR lpszType,
30
                                                           LPTSTR lpszName,LONG_PTR lParam)
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")) ){
48
                if( (h = LoadResource(hm,resinfo)) && (pparm = LockResource(h)) )
49
                        res = readPARM(pparm,&gdata->parm,reason,1 /*Windows format resource*/);
50
        }else if( readobfusc && (resinfo = FindResource(hm,MAKEINTRESOURCE(OBFUSCDATA_ID),RT_RCDATA)) ){
51
                if( (h = LoadResource(hm,resinfo)) && (pparm = 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);
55
                        byte* copy = malloc(resSize);
56
                        memcpy(copy, pparm, resSize);
57
                        obfusc(copy, resSize);
58
                        res = readPARM(copy,&gdata->parm,reason,1);
59
                        free(copy);
85 toby 60
                }
61
        }
87 toby 62
        return res;
2 toby 63
}
64
 
65
// see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibraryex.asp
66
 
147 dmarschall 67
static Boolean read8bfplugin(StandardFileReply *sfr,char **reason){
68
        unsigned char magic[2];
69
        long count;
70
        Handle h;
71
        Boolean res = false;
72
        FILEREF refnum;
73
        int i;
74
 
75
        if(!FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum)){
76
                // check DOS EXE magic number
77
                count = 2;
78
                if(!FSRead(refnum,&count,magic) && magic[0]=='M' && magic[1]=='Z'){
79
                        if(!GetEOF(refnum,&count) && count < 256L<<10){ // sanity check file size < 256K
80
                                if( (h = readfileintohandle(refnum)) ){
81
                                        long *q = (long*)PILOCKHANDLE(h,false);
82
 
83
                                        // look for signature at start of valid PARM resource
84
                                        // This signature is observed in Filter Factory standalones.
85
                                        for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
86
                                                if( ((q[0] == PARM_SIZE) ||
87
                                                     (q[0] == PARM_SIZE_PREMIERE) ||
88
                                                     (q[0] == PARM_SIG_FOUNDRY_OLD)) && q[1] == 1
89
                                                        && (res = readPARM((char*)q, &gdata->parm, reason, 1 /*Windows format resource*/)) )
90
                                                {
91
                                                }
92
 
93
                                        PIDISPOSEHANDLE(h);
94
                                }
95
                        }
96
                } // else no point in proceeding
97
                FSClose(refnum);
98
        }else
99
                *reason = "Could not open file.";
100
        return res;
101
}
102
 
2 toby 103
Boolean loadfile(StandardFileReply *sfr,char **reason){
104
        Boolean readok = false;
105
        HMODULE hm;
106
 
107
        sfr->sfFile.name[*sfr->sfFile.name+1] = 0; // add terminating null
108
 
109
        if(sfr->nFileExtension){
145 dmarschall 110
                if ((!strcasecmp((char*)sfr->sfFile.name + 1 + sfr->nFileExtension,"8bf")) ||
111
                    (!strcasecmp((char*)sfr->sfFile.name + 1 + sfr->nFileExtension,"prm"))) {
112
                        // File extention is 8bf or prm
23 toby 113
                        if( (hm = LoadLibraryEx((char*)sfr->sfFile.name+1,NULL,LOAD_LIBRARY_AS_DATAFILE)) ){
87 toby 114
                                if(readPARMresource(hm,reason,0)){
2 toby 115
                                        if(gdata->parm.iProtected)
85 toby 116
                                                *reason = "The filter is protected.";
2 toby 117
                                        else
118
                                                readok = gdata->parmloaded = true;
85 toby 119
                                }else
120
                                        *reason = "It is not a standalone filter made by Filter Factory/Filter Foundry.";
2 toby 121
                                FreeLibrary(hm);
122
                        }else{
147 dmarschall 123
                                //*reason = "Could not open file (LoadLibrary failed).";
124
                                //dbglasterror("LoadLibraryEx");
125
 
126
                                // Maybe it is 16 bit Windows? Try to find the resource
127
                                if (!(readok = read8bfplugin(sfr, reason))) {
128
                                        *reason = "PARM resource was not found in this plugin file";
129
                                }
2 toby 130
                        }
131
                }else{
145 dmarschall 132
                        // Try to read as AFS/PFF/TXT file
23 toby 133
                        if( (readok = readfile(sfr,reason)) )
2 toby 134
                                gdata->parmloaded = false;
135
                }
136
        }
137
 
138
        return readok;
139
}