Subversion Repositories filter_foundry

Rev

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