Subversion Repositories filter_foundry

Rev

Rev 85 | Rev 145 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-7 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by  
  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.  
  15.     You should have received a copy of the GNU General Public License  
  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 <Endian.h>
  23. #include <Resources.h>
  24.  
  25. #include "file_compat.h"
  26.  
  27. Boolean readPARMresource(HMODULE hm,char **reason,int readobfusc){
  28.         Boolean res = false;
  29.         Handle h;
  30.  
  31.         if( !(h = Get1Resource(PARM_TYPE,PARM_ID))
  32.           && readobfusc
  33.           && (h = Get1Resource('DATA',OBFUSCDATA_ID)) ){
  34.                 HLock(h);
  35.                 obfusc((unsigned char*)*h,GetHandleSize(h));
  36.         }
  37.         if(h){
  38.                 HLock(h);
  39.                 res = readPARM(*h, &gdata->parm, reason, 0 /*Mac format resource*/);
  40.                 ReleaseResource(h);
  41.         }
  42.         return res;
  43. }
  44.  
  45. static Boolean readmacplugin(StandardFileReply *sfr,char **reason){
  46.         Boolean res = false;
  47.         short rrn = FSpOpenResFile(&sfr->sfFile,fsRdPerm);
  48.        
  49.         if(rrn != -1){
  50.                 if(readPARMresource(NULL,reason,0))
  51.                         res = true;
  52.                 CloseResFile(rrn);
  53.         }else
  54.                 *reason = "Could not open file.";
  55.         return res;
  56. }
  57.  
  58. static Boolean read8bfplugin(StandardFileReply *sfr,char **reason){
  59.         unsigned char magic[2];
  60.         long count;
  61.         Handle h;
  62.         Boolean res = false;
  63.         short refnum;
  64.         int i;
  65.        
  66.         if(!FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum)){
  67.                 // check DOS EXE magic number
  68.                 count = 2;
  69.                 if(!FSRead(refnum,&count,magic) && magic[0]=='M' && magic[1]=='Z'){
  70.                         if(!GetEOF(refnum,&count) && count < 256L<<10){ // sanity check file size < 256K
  71.                                 if( (h = readfileintohandle(refnum)) ){
  72.                                         long *q = (long*)PILOCKHANDLE(h,false);
  73.                                        
  74.                                         // look for signature at start of valid PARM resource
  75.                                         // This signature is observed in Filter Factory standalones.
  76.                                         for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
  77.                                                 if( EndianS32_LtoN(q[0]) == PARM_SIZE && EndianS32_LtoN(q[1]) == 1
  78.                                                         && (res = readPARM((char*)q, &gdata->parm, reason, 1 /*Windows format resource*/)) )
  79.                                                 {
  80.                                                         // these are the only numeric fields we *have* to swap
  81.                                                         // all the rest are flags which (if we're careful) will work in either ordering
  82.                                                         for(i = 0; i < 8; ++i)
  83.                                                                 slider[i] = EndianS32_LtoN(slider[i]);
  84.                                                 }
  85.  
  86.                                         PIDISPOSEHANDLE(h);
  87.                                 }
  88.                         }
  89.                 } // else no point in proceeding
  90.                 FSClose(refnum);
  91.         }else
  92.                 *reason = "Could not open file.";
  93.         return res;
  94. }
  95.  
  96. Boolean loadfile(StandardFileReply *sfr,char **reason){
  97.         Boolean readok = false;
  98.         FInfo fndrInfo;
  99.  
  100.         if(!FSpGetFInfo(&sfr->sfFile,&fndrInfo)){
  101.                 // first try to read text parameters (AFS, TXT)
  102.                 if( (readok = readfile(sfr,reason)) )
  103.                         gdata->parmloaded = false;
  104.                         // then try plugin formats (Mac first, then Windows .8bf DLL)
  105.                 else if( (readok = readmacplugin(sfr,reason) || read8bfplugin(sfr,reason)) ){
  106.                         if(gdata->parm.iProtected){
  107.                                 *reason = "The filter is protected.";
  108.                                 return false;
  109.                         }else
  110.                                 gdata->parmloaded = true;
  111.                 }else
  112.                         *reason = "It is not a text parameter (AFS) file, nor a standalone Mac/PC filter made by Filter Factory/Filter Foundry.";
  113.         }
  114.  
  115.         return readok;
  116. }
  117.