Subversion Repositories filter_foundry

Rev

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