Subversion Repositories filter_foundry

Rev

Rev 549 | Rev 552 | 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-2009 Toby Thain, toby@telegraphics.net
  4.     Copyright (C) 2018-2023 Daniel Marschall, ViaThinkSoft
  5.  
  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.
  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.  
  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
  19. */
  20.  
  21. #include "ff.h"
  22.  
  23. #include "file_compat.h"
  24.  
  25. #ifdef MAC_ENV
  26. #include <Endian.h>
  27. #else
  28. int EndianS32_LtoN(int num) {
  29.         return ((num>>24)&0xff) +      // move byte 3 to byte 0
  30.                ((num<<8)&0xff0000) +   // move byte 1 to byte 2
  31.                ((num>>8)&0xff00) +     // move byte 2 to byte 1
  32.                ((num<<24)&0xff000000); // byte 0 to byte 3
  33. }
  34. #endif
  35.  
  36. #define BUFSIZE 4L<<10
  37. #define MAXLINE 0x200
  38.  
  39. Boolean readparams_afs_pff(Handle h, TCHAR**reason){
  40.         Boolean res = false;
  41.         char linebuf[MAXLINE] = { 0 };
  42.         char curexpr[MAXEXPR] = { 0 };
  43.         char *p, *dataend, *q;
  44.         char c;
  45.         int linecnt, lineptr, exprcnt;
  46.  
  47.         if (!h) return false;
  48.  
  49.         p = PILOCKHANDLE(h,false);
  50.         dataend = p + PIGETHANDLESIZE(h);
  51.  
  52.         q = curexpr;
  53.         linecnt = exprcnt = lineptr = 0;
  54.  
  55.         while(p < dataend){
  56.  
  57.                 c = *p++;
  58.  
  59.                 if(c==CR || c==LF){ /* detected end of line */
  60.  
  61.                         /* look ahead to see if we need to skip a line feed (DOS CRLF EOL convention) */
  62.                         if(p < dataend && c == CR && *p == LF)
  63.                                 ++p;
  64.  
  65.                         linebuf[lineptr] = '\0'; /* add terminating NUL to line buffer */
  66.  
  67.                         /* process complete line */
  68.                         if(linecnt==0){
  69.                                 if(strcmp(linebuf,"%RGB-1.0") != 0){
  70.                                         // Note: We don't set *reason, because we cannot be sure if it is a valid file in regards to a different data type
  71.                                         // We only set *Reason, if we are sure that it is an AFS file which is indeed wrong.
  72.                                         break;
  73.                                 }
  74.                         }else if(linecnt<=8){
  75.                                 int v;
  76.                                 v = atoi(linebuf);
  77.                                 if (v < 0) v = 0;
  78.                                 else if (v > 255) v = 255;
  79.                                 gdata->parm.val[linecnt-1] = (uint8_t)v;
  80.                         }else{
  81.                                 if(lineptr){
  82.                                         /* it's not an empty line; append it to current expr string */
  83.                                         if( (q-curexpr) + lineptr >= MAXEXPR) {
  84.                                                 if (reason) *reason = FF_GetMsg_Cpy(MSG_EXPRESSION1024_FOUND_ID);
  85.                                                 break;
  86.                                         }
  87.                                         q = cat(q,linebuf);
  88.                                 }else{
  89.                                         /* it's an empty line: we've completed the expr string */
  90.                                         *q = '\0';
  91.                                         strcpy(gdata->parm.szFormula[exprcnt], curexpr);
  92.  
  93.                                         if(++exprcnt == 4){
  94.                                                 res = true;
  95.                                                 break; /* got everything we want */
  96.                                         }
  97.  
  98.                                         q = curexpr; /* empty current expr, ready for next one */
  99.                                 }
  100.                         }
  101.  
  102.                         ++linecnt;
  103.                         lineptr = 0;
  104.                 }else{
  105.                         /* store character */
  106.                         if(c=='\\'){ /* escape sequence */
  107.                                 if(p < dataend){
  108.                                         c = *p++;
  109.                                         switch(c){
  110.                                         case 'r':
  111.                                                 #if WIN_ENV
  112.                                                 c = CR;
  113.                                                 if (lineptr < MAXLINE-1)
  114.                                                         linebuf[lineptr++] = c;
  115.                                                 c = LF;
  116.                                                 #else
  117.                                                 c = CR;
  118.                                                 #endif
  119.                                                 break;
  120.                                         case '\\': break;
  121.                                         //default:
  122.                                         //      if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("Unknown escape sequence in input.")); // TODO (Not so important): TRANSLATE
  123.                                         }
  124.                                 }//else if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("truncated escape sequence ends input")); // TODO (Not so important): TRANSLATE
  125.                         }
  126.  
  127.                         if(lineptr < MAXLINE-1)
  128.                                 linebuf[lineptr++] = c;
  129.                 }
  130.         }
  131.  
  132.         PIUNLOCKHANDLE(h);
  133.  
  134.         return res;
  135. }
  136.  
  137. void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) {
  138.         int i;
  139.  
  140.         photoshop->cbSize = sizeof(PARM_T);
  141.         photoshop->standalone = premiere->standalone;
  142.         for (i=0;i<8;++i)
  143.                 photoshop->val[i] = premiere->val[i];
  144.         photoshop->popDialog = premiere->popDialog;
  145.         photoshop->unknown1 = premiere->unknown1;
  146.         photoshop->unknown2 = premiere->unknown2;
  147.         photoshop->unknown3 = premiere->unknown3;
  148.         for (i=0;i<4;++i)
  149.                 photoshop->map_used[i] = premiere->map_used[i];
  150.         for (i=0;i<8;++i)
  151.                 photoshop->ctl_used[i] = premiere->ctl_used[i];
  152.         sprintf(photoshop->szCategory, "Filter Factory"); // Premiere plugins do not have a category attribute
  153.         photoshop->iProtected = 0; // Premiere plugins do not have a protect flag
  154.         memcpy((void*)photoshop->szTitle, (void*)premiere->szTitle, sizeof(photoshop->szTitle));
  155.         memcpy((void*)photoshop->szCopyright, (void*)premiere->szCopyright, sizeof(photoshop->szCopyright));
  156.         memcpy((void*)photoshop->szAuthor, (void*)premiere->szAuthor, sizeof(photoshop->szAuthor));
  157.         for (i=0;i<4;++i)
  158.                 memcpy((void*)photoshop->szMap[i], (void*)premiere->szMap[i], sizeof(photoshop->szMap[i]));
  159.         for (i=0;i<8;++i)
  160.                 memcpy((void*)photoshop->szCtl[i], (void*)premiere->szCtl[i], sizeof(photoshop->szCtl[i]));
  161.  
  162.         if (premiere->singleExpression) {
  163.                 memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  164.                 memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  165.                 memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  166.                 memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  167.         } else {
  168.                 memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[2], sizeof(photoshop->szFormula[2]));
  169.                 memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[1], sizeof(photoshop->szFormula[1]));
  170.                 memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[0], sizeof(photoshop->szFormula[0]));
  171.                 memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  172.         }
  173. }
  174.  
  175. char* _ffx_read_str(char** q) {
  176.         uint32_t len;
  177.         char* val;
  178.  
  179.         len = *((uint32_t*)*q);
  180.         *q += sizeof(uint32_t);
  181.         val = (char*)malloc((size_t)len + 1);
  182.         if (val != NULL) {
  183.                 memcpy(val, (char*)*q, len);
  184.                 val[len] = '\0';
  185.         }
  186.         *q += len;
  187.         return val;
  188. }
  189.  
  190. Boolean readfile_ffx(StandardFileReply* sfr, TCHAR** reason) {
  191.         Handle h;
  192.         Boolean res = false;
  193.         FILEREF refnum;
  194.         uint32_t len;
  195.         char* val;
  196.         int format_version = -1;
  197.         int i;
  198.  
  199.         UNREFERENCED_PARAMETER(reason);
  200.  
  201.         if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
  202.                 if ((h = readfileintohandle(refnum))) {
  203.                         char* q = (char*)PILOCKHANDLE(h, false);
  204.  
  205.                         len = *((uint32_t*)q);
  206.                         if (len == 6) {
  207.                                 val = _ffx_read_str(&q);
  208.                                 if (strcmp(val, "FFX1.0") == 0) format_version = 10;
  209.                                 else if (strcmp(val, "FFX1.1") == 0) format_version = 11;
  210.                                 else if (strcmp(val, "FFX1.2") == 0) format_version = 12;
  211.                                 free(val);
  212.                                 if (format_version > 0) {
  213.                                         simplewarning_id(MSG_FILTERS_UNLIMITED_WARNING_ID);
  214.  
  215.                                         val = _ffx_read_str(&q);
  216.                                         if (strlen(val) >= sizeof(gdata->parm.szTitle)) {
  217.                                                 val[sizeof(gdata->parm.szTitle) - 1] = '\0';
  218.                                         }
  219.                                         strcpy(gdata->parm.szTitle, val);
  220.                                         free(val);
  221.  
  222.                                         val = _ffx_read_str(&q);
  223.                                         if (strlen(val) >= sizeof(gdata->parm.szCategory)) {
  224.                                                 val[sizeof(gdata->parm.szCategory) - 1] = '\0';
  225.                                         }
  226.                                         strcpy(gdata->parm.szCategory, val);
  227.                                         free(val);
  228.  
  229.                                         val = _ffx_read_str(&q);
  230.                                         if (strlen(val) >= sizeof(gdata->parm.szAuthor)) {
  231.                                                 val[sizeof(gdata->parm.szAuthor) - 1] = '\0';
  232.                                         }
  233.                                         strcpy(gdata->parm.szAuthor, val);
  234.                                         free(val);
  235.  
  236.                                         val = _ffx_read_str(&q);
  237.                                         if (strlen(val) >= sizeof(gdata->parm.szCopyright)) {
  238.                                                 val[sizeof(gdata->parm.szCopyright) - 1] = '\0';
  239.                                         }
  240.                                         strcpy(gdata->parm.szCopyright, val);
  241.                                         free(val);
  242.  
  243.                                         // Channels I, R, G, B, A
  244.                                         for (i = 0; i < 4; i++) {
  245.                                                 val = _ffx_read_str(&q);
  246.                                                 if (i == 0) {
  247.                                                         char* val2 = _ffx_read_str(&q);
  248.                                                         if (strcmp(val, "0") != 0) {
  249.                                                                 // "Intro channel" existing
  250.                                                                 // C++ wrong warning: Using uninitialized memory "val2" (C6001)
  251.                                                                 #pragma warning(suppress : 6001)
  252.                                                                 char* combined = (char*)malloc(strlen(val) + strlen(",") + strlen(val2) + 1/*NUL byte*/);
  253.                                                                 if (combined != NULL) {
  254.                                                                         sprintf(combined, "%s,%s", val, val2);
  255.                                                                         free(val);
  256.                                                                         free(val2);
  257.                                                                         val = combined;
  258.                                                                 }
  259.                                                         }
  260.                                                         else {
  261.                                                                 free(val);
  262.                                                                 val = val2;
  263.                                                         }
  264.                                                 }
  265.                                                 if (strlen(val) >= sizeof(gdata->parm.szFormula[i])) {
  266.                                                         if (i == 0) {
  267.                                                                 simplealert_id(MSG_FORMULA_IR_1023_TRUNCATED_ID);
  268.                                                         }
  269.                                                         else if (i == 1) {
  270.                                                                 simplealert_id(MSG_FORMULA_G_1023_TRUNCATED_ID);
  271.                                                         }
  272.                                                         else if (i == 2) {
  273.                                                                 simplealert_id(MSG_FORMULA_B_1023_TRUNCATED_ID);
  274.                                                         }
  275.                                                         else if (i == 3) {
  276.                                                                 simplealert_id(MSG_FORMULA_A_1023_TRUNCATED_ID);
  277.                                                         }
  278.                                                         // C++ wrong warning: Buffer overflow (C6386)
  279.                                                         #pragma warning(suppress : 6386)
  280.                                                         val[sizeof(gdata->parm.szFormula[i]) - 1] = '\0';
  281.                                                 }
  282.                                                 strcpy(gdata->parm.szFormula[i], val);
  283.                                                 free(val);
  284.                                         }
  285.  
  286.                                         // Sliders
  287.                                         for (i = 0; i < 8; i++) {
  288.                                                 char* sliderName;
  289.                                                 int v;
  290.                                                 val = _ffx_read_str(&q);
  291.                                                 sliderName = val;
  292.                                                 if (format_version >= 12) {
  293.                                                         // Format FFX1.2 has prefixes {S} = Slider, {C} = Checkbox, none = Slider
  294.                                                         if ((sliderName[0] == '{') && (sliderName[1] == 'S') && (sliderName[2] == '}')) sliderName += 3;
  295.                                                         else if ((sliderName[0] == '{') && (sliderName[1] == 'C') && (sliderName[2] == '}')) sliderName += 3;
  296.                                                 }
  297.                                                 if (strlen(val) >= sizeof(gdata->parm.szCtl[i])) {
  298.                                                         val[sizeof(gdata->parm.szCtl[i]) - 1] = '\0';
  299.                                                 }
  300.                                                 strcpy(gdata->parm.szCtl[i], sliderName);
  301.                                                 free(val);
  302.                                                 gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q);
  303.                                                 q += sizeof(byte);
  304.                                                 gdata->parm.val[i] = *((uint32_t*)q);
  305.                                                 v = *((uint32_t*)q);
  306.                                                 if (v < 0) v = 0;
  307.                                                 else if (v > 255) v = 255;
  308.                                                 gdata->parm.val[i] = (uint8_t)v;
  309.                                                 q += sizeof(uint32_t);
  310.                                         }
  311.  
  312.                                         // Maps (are not part of the format!)
  313.                                         strcpy(gdata->parm.szMap[0], "Map 0:");
  314.                                         strcpy(gdata->parm.szMap[1], "Map 1:");
  315.                                         strcpy(gdata->parm.szMap[2], "Map 2:");
  316.                                         strcpy(gdata->parm.szMap[3], "Map 3:");
  317.  
  318.                                         res = true;
  319.                                 }
  320.                         }
  321.                         PIDISPOSEHANDLE(h);
  322.                 }
  323.                 FSClose(refnum);
  324.         }
  325.  
  326.         if (res) gdata->obfusc = false;
  327.         return res;
  328. }
  329.  
  330. Boolean readfile_8bf(StandardFileReply *sfr, TCHAR**reason){
  331.         unsigned char magic[2];
  332.         FILECOUNT count;
  333.         Handle h;
  334.         Boolean res = false;
  335.         FILEREF refnum;
  336.  
  337.         if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){
  338.                 // check DOS EXE magic number
  339.                 count = 2;
  340.                 if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){
  341.                         if(GetEOF(refnum,(FILEPOS*)&count) == noErr && count < 4096L<<10){ // sanity check file size < 4MiB (note that "Debug" builds can have approx 700 KiB while "Release" builds have approx 300 KiB)
  342.                                 if( (h = readfileintohandle(refnum)) ){
  343.                                         long *q = (long*)PILOCKHANDLE(h,false);
  344.  
  345.                                         // look for signature at start of valid PARM resource
  346.                                         // This signature is observed in Filter Factory standalones.
  347.                                         for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
  348.                                         {
  349.                                                 res = readPARM(&gdata->parm, (Ptr)q);
  350.                                                 if (res) break;
  351.                                         }
  352.  
  353.                                         PIDISPOSEHANDLE(h);
  354.                                 }
  355.                         }
  356.                 } // else no point in proceeding
  357.                 FSClose(refnum);
  358.         }else
  359.                 if (reason) *reason = FF_GetMsg_Cpy(MSG_CANNOT_OPEN_FILE_ID);
  360.  
  361.         if (res) gdata->obfusc = false;
  362.         return res;
  363. }
  364.  
  365. Boolean readPARM(PARM_T* pparm, Ptr p){
  366.         Boolean towin, tomac, fromwin, frommac;
  367.         unsigned int signature = *((unsigned int*)p);
  368.         unsigned int standalone = *((unsigned int*)p+1);
  369.  
  370.         // Find out our OS ("reader") the OS of the plugin ("source")
  371.         #ifdef MAC_ENV
  372.         towin = false;
  373.         tomac = true;
  374.         fromwin = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
  375.                 (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
  376.                 (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
  377.         frommac = ((signature == PARM_SIZE) ||
  378.                 (signature == PARM_SIZE_PREMIERE) ||
  379.                 (signature == PARM_SIG_MAC)) && standalone == 1;
  380.         #else
  381.         towin = true;
  382.         tomac = false;
  383.         fromwin = ((signature == PARM_SIZE) ||
  384.                 (signature == PARM_SIZE_PREMIERE) ||
  385.                 (signature == PARM_SIG_MAC)) && standalone == 1;
  386.         frommac = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
  387.                 (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
  388.                 (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
  389.         #endif
  390.  
  391.         // Is it a valid signature?
  392.         if (!fromwin && !frommac) {
  393.                 // No valid signature found
  394.                 return false;
  395.         }
  396.  
  397.         // Does it come from Premiere or Photoshop?
  398.         // Initialize pparm
  399.         if ((signature == PARM_SIZE_PREMIERE) || (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE)) {
  400.                 // It comes from Premiere. Swap R and B channel and convert to a Photoshop PARM_T
  401.                 convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
  402.         } else {
  403.                 // It is already Photoshop. Just copy to pparm.
  404.                 memcpy(pparm, p, sizeof(PARM_T));
  405.         }
  406.  
  407.         // Do we need to do string conversion?
  408.         if (frommac) {
  409.                 int i;
  410.  
  411.                 /* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */
  412.                 myp2cstr((unsigned char*)pparm->szCategory);
  413.                 myp2cstr((unsigned char*)pparm->szTitle);
  414.                 myp2cstr((unsigned char*)pparm->szCopyright);
  415.                 myp2cstr((unsigned char*)pparm->szAuthor);
  416.                 for (i = 0; i < 4; ++i)
  417.                         myp2cstr((unsigned char*)pparm->szMap[i]);
  418.                 for (i = 0; i < 8; ++i)
  419.                         myp2cstr((unsigned char*)pparm->szCtl[i]);
  420.         }
  421.  
  422.         // Case #1: Mac is reading Windows (Win16/32/64) plugin
  423.         if (fromwin && tomac) {
  424.                 size_t i;
  425.  
  426.                 // Convert copyright CRLF to CR (actually, just removing LF)
  427.                 char copyrightCRLF[256] = { 0 };
  428.                 char* pCopyright = &copyrightCRLF[0];
  429.                 for (i = 0; i < strlen(pparm->szCopyright); i++) {
  430.                         if (pparm->szCopyright[i] != LF) {
  431.                                 *pCopyright++ = pparm->szCopyright[i];
  432.                         }
  433.                 }
  434.                 *pCopyright++ = '\0';
  435.                 strcpy(pparm->szCopyright, copyrightCRLF);
  436.  
  437.                 // these are the only numeric fields we *have* to swap
  438.                 // all the rest are bool_t flags which (if we're careful) will work in either ordering
  439.                 for (i = 0; i < 8; ++i)
  440.                         pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
  441.         }
  442.  
  443.         // Case #2: Mac is reading Mac (in case the normal resource extraction didn't work)
  444.         // Nothing to do
  445.  
  446.         // Case #3: Windows is reading a Windows plugin (if Resource API failed, e.g. Win64 tries to open Win16 NE file or Win32 tries to open Win64 file)
  447.         // Nothing to do
  448.  
  449.         // Case #4: Windows is reading an old FilterFactory Mac file
  450.         // Note: You must read the ".rsrc" resource fork, not the standalone binary!
  451.         if (frommac && towin) {
  452.                 size_t i;
  453.  
  454.                 // Convert CR in the copyright field to CRLF.
  455.                 char copyrightCRLF[256] = { 0 };
  456.                 char* pCopyright = &copyrightCRLF[0];
  457.                 for (i = 0; i < strlen(pparm->szCopyright); i++) {
  458.                         *pCopyright++ = pparm->szCopyright[i];
  459.                         if (pparm->szCopyright[i] == CR) {
  460.                                 *pCopyright++ = LF;
  461.                         }
  462.                 }
  463.                 *pCopyright++ = '\0';
  464.                 strcpy(pparm->szCopyright, copyrightCRLF);
  465.  
  466.                 // these are the only numeric fields we *have* to swap
  467.                 // all the rest are bool_t flags which (if we're careful) will work in either ordering
  468.                 for (i = 0; i < 8; ++i)
  469.                         pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
  470.         }
  471.  
  472.         return true;
  473. }
  474.  
  475. Handle readfileintohandle(FILEREF r){
  476.         FILEPOS n;
  477.         Handle h;
  478.         Ptr p;
  479.  
  480.         if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){
  481.                 p = PILOCKHANDLE(h,false);
  482.                 if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){
  483.                         PIUNLOCKHANDLE(h);
  484.                         return h;
  485.                 }
  486.                 PIDISPOSEHANDLE(h);
  487.         }
  488.         return NULL;
  489. }
  490.  
  491. Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) {
  492.         size_t i;
  493.         for (i = 0; i < strlen(line); i++) {
  494.                 if (line[i] == '?') break; // avoid that "a?b:c" is detected as key
  495.                 if (line[i] == ':') {
  496.                         // Note: We are ignoring whitespaces, i.e. " A :" != "A:" (TODO: should we change this?)
  497.                         if ((searchkey == NULL) || ((i == strlen(searchkey)) && (memcmp(line, searchkey, i) == 0))) {
  498.                                 i++; // jump over ':' char
  499.                                 //while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left
  500.                                 *content = line + i;
  501.                                 return true;
  502.                         }
  503.                 }
  504.         }
  505.         *content = line;
  506.         return false;
  507. }
  508.  
  509. void _ffdcomp_removebrackets(char* x, char* maxptr) {
  510.         char* closingBracketPos = NULL;
  511.         Boolean openingBracketFound = false;
  512.         if (x[0] == '[') {
  513.                 openingBracketFound = true;
  514.         }
  515.         x[0] = ':';
  516.         x++;
  517.         while (x < maxptr) {
  518.                 if ((!openingBracketFound) && (x[0] == '[')) {
  519.                         openingBracketFound = true;
  520.                         x[0] = ' ';
  521.                 }
  522.                 else if (openingBracketFound) {
  523.                         if (x[0] == ']') {
  524.                                 closingBracketPos = x;
  525.                         }
  526.                         else if ((x[0] == CR) || (x[0] == LF)) {
  527.                                 if (closingBracketPos) closingBracketPos[0] = ' '; // last closing pos before CR/LF
  528.                                 break;
  529.                         }
  530.                 }
  531.                 x++;
  532.         }
  533. }
  534.  
  535. // isFormula=false => outputFile is C string. TXT linebreaks become spaces.
  536. // isFormula=true  => outputFile is C string. TXT line breaks become CRLF line breaks
  537. Boolean _picoReadProperty(char* inputFile, size_t maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) {
  538.         size_t i;
  539.         char* outputwork;
  540.         char* sline;
  541.         char* svalue;
  542.         char* inputwork;
  543.         char* inputworkinitial;
  544.         outputwork = outputFile;
  545.         sline = NULL;
  546.         svalue = NULL;
  547.         // Check parameters
  548.         if (maxOutput == 0) return false;
  549.         if (inputFile == NULL) return false;
  550.         // Let input memory be read-only, +1 for terminal zero
  551.         //char* inputwork = inputFile;
  552.         inputwork = (char*)malloc((size_t)maxInput + 1/*NUL byte*/);
  553.         inputworkinitial = inputwork;
  554.         if (inputwork == NULL) return false;
  555.         memcpy(inputwork, inputFile, maxInput);
  556.         inputwork[maxInput] = '\0'; // otherwise strstr() will crash
  557.  
  558.         // Transform "FFDecomp" TXT file into the similar "PluginCommander" TXT file
  559.         if (strstr(inputwork, "Filter Factory Plugin Information:")) {
  560.                 char* x;
  561.                 char* k1;
  562.                 char* k2;
  563.                 // Metadata:
  564.                 x = strstr(inputwork, "CATEGORY:");
  565.                 if (x) memcpy(x, "Category:", strlen("Category:"));
  566.                 x = strstr(inputwork, "TITLE:");
  567.                 if (x) memcpy(x, "Title:", strlen("Title:"));
  568.                 x = strstr(inputwork, "COPYRIGHT:");
  569.                 if (x) memcpy(x, "Copyright:", strlen("Copyright:"));
  570.                 x = strstr(inputwork, "AUTHOR:");
  571.                 if (x) memcpy(x, "Author:", strlen("Author:"));
  572.                 // Controls:
  573.                 for (i = 0; i < 8; i++) {
  574.                         k1 = (char*)malloc(strlen("Control X:") + 1/*NUL byte*/);
  575.                         sprintf(k1, "Control %d:", (int)i);
  576.                         x = strstr(inputwork, k1);
  577.                         if (x) {
  578.                                 k2 = (char*)malloc(strlen("ctl[X]:   ") + 1/*NUL byte*/);
  579.                                 sprintf(k2, "ctl[%d]:   ", (int)i);
  580.                                 memcpy(x, k2, strlen(k2));
  581.                                 x += strlen("ctl[X]");
  582.                                 _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
  583.                                 free(k2);
  584.                         }
  585.                         free(k1);
  586.                 }
  587.                 // Maps:
  588.                 for (i = 0; i < 4; i++) {
  589.                         k1 = (char*)malloc(strlen("Map X:") + 1/*NUL byte*/);
  590.                         sprintf(k1, "Map %d:", (int)i);
  591.                         x = strstr(inputwork, k1);
  592.                         if (x) {
  593.                                 k2 = (char*)malloc(strlen("map[X]:") + 1/*NUL byte*/);
  594.                                 sprintf(k2, "map[%d]:", (int)i);
  595.                                 memcpy(x, k2, strlen(k2));
  596.                                 x += strlen("map[X]");
  597.                                 _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
  598.                                 free(k2);
  599.                         }
  600.                         free(k1);
  601.                 }
  602.                 // Convert all '\r' to '\n' for the next step to be easier
  603.                 for (i = 0; i < maxInput; i++) {
  604.                         if (inputworkinitial[i] == CR) inputworkinitial[i] = LF;
  605.                 }
  606.                 x = strstr(inputwork, "\nR=\n");
  607.                 if (x) memcpy(x, "\nR:\n", strlen("\nR:\n"));
  608.                 x = strstr(inputwork, "\nG=\n");
  609.                 if (x) memcpy(x, "\nG:\n", strlen("\nG:\n"));
  610.                 x = strstr(inputwork, "\nB=\n");
  611.                 if (x) memcpy(x, "\nB:\n", strlen("\nB:\n"));
  612.                 x = strstr(inputwork, "\nA=\n");
  613.                 if (x) memcpy(x, "\nA:\n", strlen("\nA:\n"));
  614.         }
  615.         // Replace all \r and \n with \0, so that we can parse easier
  616.         for (i = 0; i < maxInput; i++) {
  617.                 if (inputworkinitial[i] == CR) inputworkinitial[i] = '\0';
  618.                 else if (inputworkinitial[i] == LF) inputworkinitial[i] = '\0';
  619.         }
  620.  
  621.         // Find line that contains out key
  622.         inputwork = inputworkinitial;
  623.         do {
  624.                 if (inputwork > inputworkinitial + maxInput) {
  625.                         // Key not found. Set output to empty string
  626.                         outputwork[0] = '\0';
  627.                         free(inputworkinitial);
  628.                         return false;
  629.                 }
  630.                 sline = inputwork;
  631.                 inputwork += strlen(sline) + 1;
  632.                 if (inputwork - 1 > inputworkinitial + maxInput) {
  633.                         // Key not found. Set output to empty string
  634.                         // TODO: will that be ever called?
  635.                         outputwork[0] = '\0';
  636.                         free(inputworkinitial);
  637.                         return false;
  638.                 }
  639.         } while (!_picoLineContainsKey(sline, &svalue, property));
  640.  
  641.         // Read line(s) until we find a line with another key, or the line end
  642.         do {
  643.                 while ((svalue[0] == ' ') || (svalue[0] == TAB)) svalue++; // Trim left
  644.                 while ((svalue[strlen(svalue) - 1] == ' ') || (svalue[strlen(svalue) - 1] == TAB)) svalue[strlen(svalue) - 1] = '\0'; // Trim right
  645.  
  646.                 if (strlen(svalue) > 0) {
  647.                         if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) {
  648.                                 size_t remaining = maxOutput - (outputwork - outputFile) - 1;
  649.                                 //printf("BUFFER FULL (remaining = %d)\n", remaining);
  650.                                 memcpy(outputwork, svalue, remaining);
  651.                                 outputwork += remaining;
  652.                                 outputwork[0] = '\0';
  653.                                 free(inputworkinitial);
  654.                                 return true;
  655.                         }
  656.                         else {
  657.                                 memcpy(outputwork, svalue, strlen(svalue));
  658.                                 outputwork += strlen(svalue);
  659.                                 if (isFormula) {
  660.                                         // Formulas: TXT line break stays line break (important if you have comments!)
  661.                                         outputwork[0] = CR;
  662.                                         outputwork[1] = LF;
  663.                                         outputwork += 2;
  664.                                 }
  665.                                 else {
  666.                                         // Everything else: TXT line breaks becomes single whitespace
  667.                                         outputwork[0] = ' ';
  668.                                         outputwork++;
  669.                                 }
  670.                         }
  671.                 }
  672.                 outputwork[0] = '\0';
  673.  
  674.                 // Process next line
  675.                 if (inputwork > inputworkinitial + maxInput) break;
  676.                 sline = inputwork;
  677.                 inputwork += strlen(sline) + 1;
  678.                 if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called?
  679.         } while (!_picoLineContainsKey(sline, &svalue, NULL));
  680.  
  681.         // Remove trailing whitespace
  682.         if (outputwork > outputFile) {
  683.                 outputwork -= 1;
  684.                 outputwork[0] = '\0';
  685.         }
  686.         free(inputworkinitial);
  687.         return true;
  688. }
  689.  
  690. Boolean readfile_picotxt_or_ffdecomp(StandardFileReply* sfr, TCHAR** reason) {
  691.         Handle h;
  692.         Boolean res = false;
  693.         FILEREF refnum;
  694.  
  695.         UNREFERENCED_PARAMETER(reason);
  696.  
  697.         if (!fileHasExtension(sfr, TEXT(".txt"))) return false;
  698.  
  699.         if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
  700.                 if ((h = readfileintohandle(refnum))) {
  701.                         FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h);
  702.                         char* q = PILOCKHANDLE(h, false);
  703.  
  704.                         char dummy[256];
  705.                         if (_picoReadProperty(q, count, "Title", dummy, sizeof(dummy), false)) {
  706.                                 int i;
  707.  
  708.                                 // Plugin infos
  709.                                 _picoReadProperty(q, count, "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle), false);
  710.                                 _picoReadProperty(q, count, "Category", gdata->parm.szCategory, sizeof(gdata->parm.szCategory), false);
  711.                                 _picoReadProperty(q, count, "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor), false);
  712.                                 _picoReadProperty(q, count, "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright), false);
  713.                                 //_picoReadProperty(q, count, "Filename", gdata->parm.xxx, sizeof(gdata->parm.xxx), false);
  714.  
  715.                                 // Expressions
  716.                                 if (!_picoReadProperty(q, count, "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]), true))
  717.                                         strcpy(gdata->parm.szFormula[0], "r");
  718.                                 if (!_picoReadProperty(q, count, "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]), true))
  719.                                         strcpy(gdata->parm.szFormula[1], "g");
  720.                                 if (!_picoReadProperty(q, count, "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]), true))
  721.                                         strcpy(gdata->parm.szFormula[2], "b");
  722.                                 if (!_picoReadProperty(q, count, "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]), true))
  723.                                         strcpy(gdata->parm.szFormula[3], "a");
  724.  
  725.                                 for (i = 0; i < 8; i++) {
  726.                                         if (gdata->parm.ctl_used[i]) {
  727.                                                 int v;
  728.                                                 char keyname[6/*strlen("ctl[X]")*/ + 1/*strlen("\0")*/], tmp[5];
  729.  
  730.                                                 // Slider names
  731.                                                 sprintf(keyname, "ctl[%d]", i);
  732.                                                 _picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false);
  733.  
  734.                                                 // Slider values
  735.                                                 sprintf(keyname, "val[%d]", i);
  736.                                                 if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
  737.                                                         sprintf(keyname, "def[%d]", i);
  738.                                                         if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
  739.                                                                 strcpy(tmp, "0");
  740.                                                         }
  741.                                                 }
  742.                                                 v = atoi(tmp);
  743.                                                 if (v < 0) v = 0;
  744.                                                 else if (v > 255) v = 255;
  745.                                                 gdata->parm.val[i] = gdata->parm.val[i] = (uint8_t)v;
  746.                                         }
  747.                                 }
  748.  
  749.                                 // Map names
  750.                                 for (i = 0; i < 4; i++) {
  751.                                         if (gdata->parm.map_used[i]) {
  752.                                                 char keyname[6/*strlen("map[X]")*/ + 1/*strlen("\0")*/];
  753.                                                 sprintf(keyname, "map[%d]", i);
  754.                                                 _picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false);
  755.                                         }
  756.                                 }
  757.  
  758.                                 res = true;
  759.                         }
  760.  
  761.                         PIUNLOCKHANDLE(h);
  762.                         PIDISPOSEHANDLE(h);
  763.                 }
  764.                 FSClose(refnum);
  765.         }
  766.  
  767.         return res;
  768. }
  769.  
  770. Boolean _gufReadProperty(char* fileContents, size_t argMaxInputLength, const char* section, const char* keyname, char* argOutput, size_t argMaxOutLength) {
  771.         size_t iTmp;
  772.         char* tmpFileContents, * tmpSection, * tmpStart, * tmpStart2, * tmpEnd, * tmpKeyname, * tmpStart3, * tmpStart4, * inputwork;
  773.  
  774.         // Check parameters
  775.         if (argMaxOutLength == 0) return false;
  776.         if (fileContents == NULL) return false;
  777.  
  778.         // Handle argMaxInputLength
  779.         //char* inputwork = fileContents;
  780.         inputwork = (char*)malloc((size_t)argMaxInputLength + 1/*NUL byte*/);
  781.         if (inputwork == NULL) return false;
  782.         memcpy(inputwork, fileContents, argMaxInputLength);
  783.         inputwork[argMaxInputLength] = '\0';
  784.  
  785.         // Prepare the input file contents to make it easier parse-able
  786.         iTmp = strlen(inputwork) + strlen("\n\n[");
  787.         tmpFileContents = (char*)malloc(iTmp + 1/*NUL byte*/);
  788.         if (tmpFileContents == NULL) return false;
  789.         sprintf(tmpFileContents, "\n%s\n[", inputwork);
  790.         for (iTmp = 0; iTmp < strlen(tmpFileContents); iTmp++) {
  791.                 if (tmpFileContents[iTmp] == '\r') tmpFileContents[iTmp] = '\n';
  792.         }
  793.  
  794.         // Find the section begin
  795.         iTmp = strlen(section) + strlen("\n[]\n");
  796.         tmpSection = (char*)malloc(iTmp + 1/*NUL byte*/);
  797.         if (tmpSection == NULL) return false;
  798.         sprintf(tmpSection, "\n[%s]\n", section);
  799.         tmpStart = strstr(tmpFileContents, tmpSection);
  800.         if (tmpStart == NULL) return false;
  801.         tmpStart += iTmp;
  802.  
  803.         // Find the end of the section and set a NULL terminator to it
  804.         iTmp = strlen(tmpStart) + strlen("\n");
  805.         tmpStart2 = (char*)malloc(iTmp + 1/*NUL byte*/);
  806.         if (tmpStart2 == NULL) return false;
  807.         sprintf(tmpStart2, "\n%s", tmpStart);
  808.         tmpEnd = strstr(tmpStart2, "\n[");
  809.         if (tmpEnd == NULL) return false;
  810.         tmpEnd[0] = '\0';
  811.  
  812.         // Find the start of the value
  813.         iTmp = strlen(keyname) + strlen("\n=");
  814.         tmpKeyname = (char*)malloc(iTmp + 1/*NUL byte*/);
  815.         if (tmpKeyname == NULL) return false;
  816.         sprintf(tmpKeyname, "\n%s=", keyname);
  817.         tmpStart3 = strstr(tmpStart2, tmpKeyname);
  818.         if (tmpStart3 == NULL) return false;
  819.         tmpStart3 += strlen("\n");
  820.         tmpStart4 = strstr(tmpStart3, "=");
  821.         if (tmpStart4 == NULL) return false;
  822.         tmpStart4 += strlen("=");
  823.  
  824.         // Find the end of the value
  825.         tmpEnd = strstr(tmpStart4, "\n");
  826.         if (tmpEnd == NULL) return false;
  827.         tmpEnd[0] = '\0';
  828.  
  829.         // Copy to output
  830.         if (strlen(tmpStart4) < argMaxOutLength + 1) argMaxOutLength = strlen(tmpStart4) + 1;
  831.         memcpy(argOutput, tmpStart4, argMaxOutLength);
  832.         argOutput[argMaxOutLength - 1] = '\0';
  833.  
  834.         // Free all temporary stuff
  835.         //for (iTmp = 0; iTmp < strlen(tmpFileContents); iTmp++) tmpFileContents[iTmp] = '\0';
  836.         free(tmpFileContents);
  837.         //for (iTmp = 0; iTmp < strlen(tmpSection); iTmp++) tmpSection[iTmp] = '\0';
  838.         free(tmpSection);
  839.         //for (iTmp = 0; iTmp < strlen(tmpKeyname); iTmp++) tmpKeyname[iTmp] = '\0';
  840.         free(tmpKeyname);
  841.         //for (iTmp = 0; iTmp < strlen(tmpStart2); iTmp++) tmpStart2[iTmp] = '\0';
  842.         free(tmpStart2);
  843.  
  844.         // Return success
  845.         return true;
  846. }
  847.  
  848. Boolean readfile_guf(StandardFileReply* sfr, TCHAR** reason) {
  849.         Handle h;
  850.         Boolean res = false;
  851.         FILEREF refnum;
  852.  
  853.         // TODO: Decode UTF-8 to ANSI (or "?" for unknown characters)
  854.  
  855.         if (!fileHasExtension(sfr, TEXT(".guf"))) return false;
  856.  
  857.         if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
  858.                 if ((h = readfileintohandle(refnum))) {
  859.                         FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h);
  860.                         char* q = PILOCKHANDLE(h, false);
  861.  
  862.                         char out[256];
  863.                         if (_gufReadProperty(q, count, "GUF", "Protocol", out, sizeof(out))) {
  864.                                 if (strcmp(out, "1") != 0) {
  865.                                         PIUNLOCKHANDLE(h);
  866.                                         PIDISPOSEHANDLE(h);
  867.                                         FSClose(refnum);
  868.                                         if (reason) *reason = FF_GetMsg_Cpy(MSG_INCOMPATIBLE_GUF_FILE_ID);
  869.                                         return false;
  870.                                 }
  871.                         }
  872.                         else {
  873.                                 PIUNLOCKHANDLE(h);
  874.                                 PIDISPOSEHANDLE(h);
  875.                                 FSClose(refnum);
  876.                                 if (reason) *reason = FF_GetMsg_Cpy(MSG_INCOMPATIBLE_GUF_FILE_ID);
  877.                                 return false;
  878.                         }
  879.                         if (_gufReadProperty(q, count, "Info", "Title", out, sizeof(out))) {
  880.                                 int i;
  881.                                 char tmp[256];
  882.                                 char* tmp2;
  883.  
  884.                                 // Plugin infos
  885.                                 _gufReadProperty(q, count, "Info", "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle));
  886.                                 _gufReadProperty(q, count, "Info", "Category", tmp, sizeof(tmp));
  887.                                 tmp2 = strrchr(tmp, '/');
  888.                                 if (tmp2 == NULL) {
  889.                                         strcpy(gdata->parm.szCategory, tmp);
  890.                                 } else {
  891.                                         strcpy(gdata->parm.szCategory, tmp2+1);
  892.                                 }
  893.                                 _gufReadProperty(q, count, "Info", "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor));
  894.                                 _gufReadProperty(q, count, "Info", "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright));
  895.                                 //_gufReadProperty(q, count, "Filter Factory", "8bf", gdata->parm.xxx, sizeof(gdata->parm.xxx));
  896.  
  897.                                 // Expressions
  898.                                 if (!_gufReadProperty(q, count, "Code", "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0])))
  899.                                         strcpy(gdata->parm.szFormula[0], "r");
  900.                                 if (!_gufReadProperty(q, count, "Code", "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1])))
  901.                                         strcpy(gdata->parm.szFormula[1], "g");
  902.                                 if (!_gufReadProperty(q, count, "Code", "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2])))
  903.                                         strcpy(gdata->parm.szFormula[2], "b");
  904.                                 if (!_gufReadProperty(q, count, "Code", "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3])))
  905.                                         strcpy(gdata->parm.szFormula[3], "a");
  906.  
  907.                                 for (i = 0; i < 8; i++) {
  908.                                         if (gdata->parm.ctl_used[i]) {
  909.                                                 int v;
  910.                                                 char keyname[9/*strlen("Control X")*/ + 1/*strlen("\0")*/], tmp[5];
  911.                                                 sprintf(keyname, "Control %d", i);
  912.  
  913.                                                 // Slider names
  914.                                                 _gufReadProperty(q, count, keyname, "Label", gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]));
  915.  
  916.                                                 // Slider values
  917.                                                 if (!_gufReadProperty(q, count, keyname, "Preset", tmp, sizeof(tmp))) {
  918.                                                         strcpy(tmp, "0");
  919.                                                 }
  920.                                                 v = atoi(tmp);
  921.                                                 if (v < 0) v = 0;
  922.                                                 else if (v > 255) v = 255;
  923.                                                 gdata->parm.val[i] = gdata->parm.val[i] = (uint8_t)v;
  924.                                         }
  925.                                 }
  926.  
  927.                                 // Map names
  928.                                 for (i = 0; i < 4; i++) {
  929.                                         if (gdata->parm.map_used[i]) {
  930.                                                 char keyname[5/*strlen("Map X")*/ + 1/*strlen("\0")*/];
  931.                                                 sprintf(keyname, "Map %d", i);
  932.                                                 _gufReadProperty(q, count, keyname, "Label", gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]));
  933.                                         }
  934.                                 }
  935.  
  936.                                 res = true;
  937.                         }
  938.  
  939.                         PIUNLOCKHANDLE(h);
  940.                         PIDISPOSEHANDLE(h);
  941.                 }
  942.                 FSClose(refnum);
  943.         }
  944.  
  945.         return res;
  946. }
  947.  
  948. Boolean readfile_afs_pff(StandardFileReply *sfr, TCHAR**reason){
  949.         FILEREF r;
  950.         Handle h;
  951.         Boolean res = false;
  952.  
  953.         if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){
  954.                 if( (h = readfileintohandle(r)) ){
  955.                         if( (res = readparams_afs_pff(h,reason)) ) {
  956.                                 if (fileHasExtension(sfr, TEXT(".pff"))) {
  957.                                         // If it is a Premiere settings file, we need to swap the channels red and blue
  958.                                         // We just swap the pointers!
  959.                                         char tmp[MAXEXPR];
  960.                                         strcpy(tmp, gdata->parm.szFormula[0]);
  961.                                         strcpy(gdata->parm.szFormula[0], gdata->parm.szFormula[2]);
  962.                                         strcpy(gdata->parm.szFormula[2], tmp);
  963.                                 }
  964.                         }
  965.  
  966.                         PIDISPOSEHANDLE(h);
  967.                 }
  968.                 FSClose(r);
  969.         }
  970.         else
  971.                 if (reason) *reason = FF_GetMsg_Cpy(MSG_CANNOT_OPEN_FILE_ID);
  972.  
  973.         return res;
  974. }
  975.  
  976. Boolean readfile_ffl(StandardFileReply* sfr, TCHAR** reason) {
  977.         FILEREF rTmp, refnum;
  978.         Handle h, hTmp;
  979.         Boolean res = false;
  980.         StandardFileReply sfrTmp;
  981.         OSErr e;
  982.         char* p, * start;
  983.         size_t est;
  984.  
  985.         if (!fileHasExtension(sfr, TEXT(".ffl"))) return false;
  986.  
  987.         if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
  988.                 if ((h = readfileintohandle(refnum))) {
  989.                         FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h);
  990.                         char* q = PILOCKHANDLE(h, false);
  991.  
  992.                         char* q2, * tmp_cur_filter_str[29], *token;
  993.                         int lineNumber = 0;
  994.                         int countFilters = 0;
  995.  
  996.                         // This is required to make strtok work, because q is not zero-terminated...
  997.                         q2 = (char*)malloc(count + 1/*NUL byte*/);
  998.                         if (q2 == NULL) {
  999.                                 PIUNLOCKHANDLE(h);
  1000.                                 PIDISPOSEHANDLE(h);
  1001.                                 FSClose(refnum);
  1002.                                 if (reason) *reason = TEXT("Out of memory"); // TODO: translate
  1003.                                 return false;
  1004.                         }
  1005.                         memcpy(q2, q, count);
  1006.                         q2[count] = '\0';
  1007.  
  1008.                         token = strtok(q2, "\n");
  1009.                         while (token != NULL) {
  1010.                                 size_t i;
  1011.                                 char* token2 = my_strdup(token);
  1012.                                 for (i = 0; i < strlen(token2); i++) {
  1013.                                         if (token2[i] == '\r') token2[i] = '\0';
  1014.                                 }
  1015.                                 if (lineNumber == 0) {
  1016.                                         if (strcmp(token2,"FFL1.0") != 0) {
  1017.                                                 free(q2);
  1018.                                                 PIUNLOCKHANDLE(h);
  1019.                                                 PIDISPOSEHANDLE(h);
  1020.                                                 FSClose(refnum);
  1021.                                                 if (reason) *reason = TEXT("Invalid file signature"); // TODO: translate
  1022.                                                 return false;
  1023.                                         }
  1024.                                 }
  1025.                                 else if (lineNumber == 1) {
  1026.                                         countFilters = atoi(token2);
  1027.                                 }
  1028.                                 else
  1029.                                 {
  1030.                                         /*
  1031.                                         * 0 filter_file1.8bf
  1032.                                         * 1 Category 1 here
  1033.                                         * 2 Filter Name 1 here
  1034.                                         * 3 Autor 1 here
  1035.                                         * 4 Copyright 1 here
  1036.                                         * 5 Map1 name or empty line to disable map
  1037.                                         * 6 Map2 name or empty line to disable map
  1038.                                         * 7 Map3 name or empty line to disable map
  1039.                                         * 8 Map4 name or empty line to disable map
  1040.                                         * 9 Slider 1
  1041.                                         * 10 Slider 2
  1042.                                         * 11 Slider 3
  1043.                                         * 12 Slider 4
  1044.                                         * 13 Slider 5
  1045.                                         * 14 Slider 6
  1046.                                         * 15 Slider 7
  1047.                                         * 16 Slider 8
  1048.                                         * 17 100
  1049.                                         * 18 110
  1050.                                         * 19 120
  1051.                                         * 20 130
  1052.                                         * 21 140
  1053.                                         * 22 150
  1054.                                         * 23 160
  1055.                                         * 24 170
  1056.                                         * 25 r
  1057.                                         * 26 g
  1058.                                         * 27 b
  1059.                                         * 28 a
  1060.                                         */
  1061.                                         int filterLineNumber = (lineNumber - 2) % 29;
  1062.                                         tmp_cur_filter_str[filterLineNumber] = token2;
  1063.                                         if (filterLineNumber == 28) {
  1064.                                                 TCHAR* curFileNameOrig;
  1065.                                                 TCHAR* curFileName;
  1066.                                                
  1067.                                                 curFileNameOrig = curFileName = (TCHAR*)malloc(MAX_PATH*sizeof(TCHAR));
  1068.  
  1069.                                                 strcpy_advance(&curFileName, sfr->sfFile.szName);
  1070.                                                 curFileName -= 4; // remove ".ffl" extension
  1071.                                                 *curFileName = (TCHAR)0;
  1072.  
  1073.                                                 xstrcat(curFileNameOrig, TEXT("__"));
  1074.                                                 curFileName += strlen("__");
  1075.  
  1076.                                                 strcpy_advance_a(&curFileName, tmp_cur_filter_str[0]);
  1077.                                                 curFileName -= 4; // remove ".8bf" extension
  1078.                                                 *curFileName = (TCHAR)0;
  1079.  
  1080.                                                 #ifdef WIN_ENV
  1081.                                                 xstrcat(curFileNameOrig, TEXT(".txt"));
  1082.                                                 #endif
  1083.  
  1084.                                                 sfrTmp.sfGood = true;
  1085.                                                 sfrTmp.sfReplacing = true;
  1086.                                                 sfrTmp.sfType = TEXT_FILETYPE;
  1087.                                                 xstrcpy(sfrTmp.sfFile.szName, curFileNameOrig);
  1088.                                                 #ifdef WIN_ENV
  1089.                                                 sfrTmp.nFileExtension = (WORD)(xstrlen(curFileNameOrig) - strlen(".txt") + 1);
  1090.                                                 #endif
  1091.                                                 sfrTmp.sfScript = smSystemScript;
  1092.  
  1093.                                                 est = 16000;
  1094.  
  1095.                                                 FSpDelete(&sfrTmp.sfFile);
  1096.                                                 if (FSpCreate(&sfrTmp.sfFile, SIG_SIMPLETEXT, TEXT_FILETYPE, sfr->sfScript) == noErr)
  1097.                                                         if (FSpOpenDF(&sfrTmp.sfFile, fsWrPerm, &rTmp) == noErr) {
  1098.                                                                 if ((hTmp = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
  1099.                                                                         PIUNLOCKHANDLE(hTmp); // should not be necessary
  1100.                                                                         if (!(e = PISETHANDLESIZE(hTmp, (int32)(est))) && (p = start = PILOCKHANDLE(hTmp, false))) {
  1101.  
  1102.                                                                                 p += sprintf(p, "Category: %s\n", tmp_cur_filter_str[1]);
  1103.                                                                                 p += sprintf(p, "Title: %s\n", tmp_cur_filter_str[2]);
  1104.                                                                                 p += sprintf(p, "Copyright: %s\n", tmp_cur_filter_str[4]);
  1105.                                                                                 p += sprintf(p, "Author: %s\n", tmp_cur_filter_str[3]);
  1106.                                                                                 p += sprintf(p, "Filename: %s\n", tmp_cur_filter_str[0]);
  1107.                                                                                 p += sprintf(p, "\n");
  1108.                                                                                 p += sprintf(p, "R:\n");
  1109.                                                                                 p += sprintf(p, "%s\n", tmp_cur_filter_str[25]);
  1110.                                                                                 p += sprintf(p, "\n");
  1111.                                                                                 p += sprintf(p, "G:\n");
  1112.                                                                                 p += sprintf(p, "%s\n", tmp_cur_filter_str[26]);
  1113.                                                                                 p += sprintf(p, "\n");
  1114.                                                                                 p += sprintf(p, "B:\n");
  1115.                                                                                 p += sprintf(p, "%s\n", tmp_cur_filter_str[27]);
  1116.                                                                                 p += sprintf(p, "\n");
  1117.                                                                                 p += sprintf(p, "A:\n");
  1118.                                                                                 p += sprintf(p, "%s\n", tmp_cur_filter_str[28]);
  1119.                                                                                 p += sprintf(p, "\n");
  1120.  
  1121.                                                                                 // Maps
  1122.                                                                                 for (i = 0; i < 4; i++) {
  1123.                                                                                         char* tmp = tmp_cur_filter_str[5 + i];
  1124.                                                                                         if (strcmp(tmp, "") != 0) {
  1125.                                                                                                 p += sprintf(p, "map[%d]: %s\n", (int)i, tmp_cur_filter_str[5+i]);
  1126.                                                                                         }
  1127.                                                                                 }
  1128.                                                                                 p += sprintf(p, "\n");
  1129.  
  1130.                                                                                 // Controls
  1131.                                                                                 for (i = 0; i < 8; i++) {
  1132.                                                                                         char* tmp = tmp_cur_filter_str[9 + i];
  1133.                                                                                         if (strcmp(tmp, "") != 0) {
  1134.                                                                                                 p += sprintf(p, "ctl[%d]: %s\n", (int)i, tmp_cur_filter_str[9 + i]);
  1135.                                                                                         }
  1136.                                                                                         tmp = tmp_cur_filter_str[17 + i];
  1137.                                                                                         if (strcmp(tmp, "") != 0) {
  1138.                                                                                                 p += sprintf(p, "val[%d]: %s\n", (int)i, tmp_cur_filter_str[17 + i]);
  1139.                                                                                         }
  1140.                                                                                 }
  1141.                                                                                 p += sprintf(p, "\n");
  1142.  
  1143.                                                                                 PIUNLOCKHANDLE(hTmp);
  1144.                                                                                 e = PISETHANDLESIZE(hTmp, (int32)(p - start)); // could ignore this error, maybe
  1145.                                                                         }
  1146.                                                                         savehandleintofile(hTmp, rTmp);
  1147.                                                                         PIDISPOSEHANDLE(hTmp);
  1148.                                                                 }
  1149.                                                                 FSClose(rTmp);
  1150.                                                         }
  1151.  
  1152.                                                 free(curFileNameOrig);
  1153.                                                 for (i = 0; i < 29; i++) {
  1154.                                                         free(tmp_cur_filter_str[i]); // free all "token2"
  1155.                                                 }
  1156.                                         }
  1157.                                 }
  1158.                                 lineNumber++;
  1159.                                 token = strtok(NULL, "\n");
  1160.                         }
  1161.  
  1162.                         free(q2);
  1163.  
  1164.                         PIUNLOCKHANDLE(h);
  1165.                         PIDISPOSEHANDLE(h);
  1166.                 }
  1167.                 FSClose(refnum);
  1168.         }
  1169.  
  1170.         // TODO: show a different message when no filters were processed for some reason...
  1171.         // TODO: It's very confusing because this shows up as error message...
  1172.         if (reason) *reason = FF_GetMsg_Cpy(MSG_FFL_CONVERTED_ID);
  1173.         return false;
  1174. }