Subversion Repositories filter_foundry

Rev

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