Subversion Repositories filter_foundry

Rev

Rev 395 | Rev 420 | 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.com.au
  4.     Copyright (C) 2018-2021 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. enum{
  37.         BUFSIZE = 4L<<10,
  38.         MAXLINE = 0x200,
  39. };
  40.  
  41. Boolean readparams_afs_pff(Handle h,char **reason){
  42.         Boolean res = false;
  43.         char linebuf[MAXLINE+1],curexpr[MAXEXPR+1],*p,*dataend,*q;
  44.         char c;
  45.         int linecnt, lineptr, exprcnt;
  46.  
  47.         if(!h){
  48.                 *reason = _strdup("readparams: Null parameter handle.");
  49.                 return false;
  50.         }
  51.  
  52.         p = PILOCKHANDLE(h,false);
  53.         dataend = p + PIGETHANDLESIZE(h);
  54.  
  55.         q = curexpr;
  56.         linecnt = exprcnt = lineptr = 0;
  57.  
  58.         //*reason = _strdup("File was too short.");
  59.         while(p < dataend){
  60.  
  61.                 c = *p++;
  62.  
  63.                 if(c==CR || c==LF){ /* detected end of line */
  64.  
  65.                         /* look ahead to see if we need to skip a line feed (DOS EOL convention) */
  66.                         if(c == CR && *p == LF && p < dataend)
  67.                                 ++p;
  68.  
  69.                         linebuf[lineptr] = 0; /* add terminating NUL to line buffer */
  70.  
  71.                         /* process complete line */
  72.                         if(linecnt==0){
  73.                                 if(strcmp(linebuf,"%RGB-1.0")){
  74.                                         // *reason = _strdup("This doesn't look like a Filter Factory file (first line is not \"%RGB-1.0\").");
  75.                                         break;
  76.                                 }
  77.                         }else if(linecnt<=8){
  78.                                 slider[linecnt-1] = atoi(linebuf);
  79.                         }else{
  80.                                 if(lineptr){
  81.                                         /* it's not an empty line; append it to current expr string */
  82.                                         if( q+lineptr > curexpr+MAXEXPR ){
  83.                                                 *reason = _strdup("Found an expression longer than 1024 characters.");
  84.                                                 break;
  85.                                         }
  86.                                         q = cat(q,linebuf);
  87.                                 }else{
  88.                                         /* it's an empty line: we've completed the expr string */
  89.                                         if(expr[exprcnt])
  90.                                                 free(expr[exprcnt]);
  91.                                         *q = 0;
  92.                                         if(!(expr[exprcnt] = my_strdup(curexpr))){
  93.                                                 *reason = _strdup("Could not get memory for expression.");
  94.                                                 break;
  95.                                         }
  96.  
  97.                                         if(++exprcnt == 4){
  98.                                                 res = true;
  99.                                                 break; /* got everything we want */
  100.                                         }
  101.  
  102.                                         q = curexpr; /* empty current expr, ready for next one */
  103.                                 }
  104.                         }
  105.  
  106.                         ++linecnt;
  107.                         lineptr = 0;
  108.                 }else{
  109.                         /* store character */
  110.                         if(c=='\\'){ /* escape sequence */
  111.                                 if(p < dataend){
  112.                                         c = *p++;
  113.                                         switch(c){
  114.                                         case 'r':
  115.                                                 #if WIN_ENV
  116.                                                 c = CR;
  117.                                                 if (lineptr < MAXLINE)
  118.                                                         linebuf[lineptr++] = c;
  119.                                                 c = LF;
  120.                                                 #else
  121.                                                 c = CR;
  122.                                                 #endif
  123.                                                 break;
  124.                                         case '\\': break;
  125.                                         //default:
  126.                                         //      if(alerts) alertuser(_strdup("Warning:"),_strdup("Unknown escape sequence in input."));
  127.                                         }
  128.                                 }//else if(alerts) alertuser(_strdup("Warning:"),_strdup("truncated escape sequence ends input"));
  129.                         }
  130.  
  131.                         if(lineptr < MAXLINE)
  132.                                 linebuf[lineptr++] = c;
  133.                 }
  134.         }
  135.  
  136.         PIUNLOCKHANDLE(h);
  137.  
  138.         return res;
  139. }
  140.  
  141. void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) {
  142.         int i;
  143.  
  144.         photoshop->cbSize = sizeof(PARM_T);
  145.         photoshop->standalone = premiere->standalone;
  146.         for (i=0;i<8;++i)
  147.                 photoshop->val[i] = premiere->val[i];
  148.         photoshop->popDialog = premiere->popDialog;
  149.         photoshop->unknown1 = premiere->unknown1;
  150.         photoshop->unknown2 = premiere->unknown2;
  151.         photoshop->unknown3 = premiere->unknown3;
  152.         for (i=0;i<4;++i)
  153.                 photoshop->map_used[i] = premiere->map_used[i];
  154.         for (i=0;i<8;++i)
  155.                 photoshop->ctl_used[i] = premiere->ctl_used[i];
  156.         sprintf(photoshop->szCategory, "Filter Factory"); // Premiere plugins do not have a category attribute
  157.         photoshop->iProtected = 0; // Premiere plugins do not have a protect flag
  158.         memcpy((void*)photoshop->szTitle, (void*)premiere->szTitle, sizeof(photoshop->szTitle));
  159.         memcpy((void*)photoshop->szCopyright, (void*)premiere->szCopyright, sizeof(photoshop->szCopyright));
  160.         memcpy((void*)photoshop->szAuthor, (void*)premiere->szAuthor, sizeof(photoshop->szAuthor));
  161.         for (i=0;i<4;++i)
  162.                 memcpy((void*)photoshop->szMap[i], (void*)premiere->szMap[i], sizeof(photoshop->szMap[i]));
  163.         for (i=0;i<8;++i)
  164.                 memcpy((void*)photoshop->szCtl[i], (void*)premiere->szCtl[i], sizeof(photoshop->szCtl[i]));
  165.  
  166.         if (premiere->singleExpression) {
  167.                 memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  168.                 memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  169.                 memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  170.                 memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  171.         } else {
  172.                 memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[2], sizeof(photoshop->szFormula[2]));
  173.                 memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[1], sizeof(photoshop->szFormula[1]));
  174.                 memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[0], sizeof(photoshop->szFormula[0]));
  175.                 memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
  176.         }
  177. }
  178.  
  179. char* _ffx_read_str(char** q) {
  180.         uint32_t len;
  181.         char* val;
  182.  
  183.         len = *((uint32_t*)*q);
  184.         *q += sizeof(uint32_t);
  185.         val = (char*)malloc(len + 1);
  186.         if (val != NULL) {
  187.                 memcpy(val, (char*)*q, len);
  188.                 val[len] = 0;
  189.         }
  190.         *q += len;
  191.         return val;
  192. }
  193.  
  194. Boolean readfile_ffx(StandardFileReply* sfr, char** reason) {
  195.         Handle h;
  196.         Boolean res = false;
  197.         FILEREF refnum;
  198.         uint32_t len;
  199.         char* val;
  200.         int format_version = -1;
  201.         int i;
  202.  
  203.         if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
  204.                 if ((h = readfileintohandle(refnum))) {
  205.                         char* q = (char*)PILOCKHANDLE(h, false);
  206.  
  207.                         len = *((uint32_t*)q);
  208.                         if (len == 6) {
  209.                                 val = _ffx_read_str(&q);
  210.                                 if (strcmp(val, "FFX1.0") == 0) format_version = 10;
  211.                                 else if (strcmp(val, "FFX1.1") == 0) format_version = 11;
  212.                                 else if (strcmp(val, "FFX1.2") == 0) format_version = 12;
  213.                                 free(val);
  214.                                 if (format_version > 0) {
  215.                                         simplewarning(_strdup("Attention! You are loading a \"Filters Unlimited\" file. Please note that Filter Foundry only implements the basic Filter Factory functions. Therefore, most \"Filters Unlimited\" filters won't work with Filter Foundry."));
  216.  
  217.                                         val = _ffx_read_str(&q);
  218.                                         strcpy(gdata->parm.szTitle, val);
  219.                                         free(val);
  220.  
  221.                                         val = _ffx_read_str(&q);
  222.                                         strcpy(gdata->parm.szCategory, val);
  223.                                         free(val);
  224.  
  225.                                         val = _ffx_read_str(&q);
  226.                                         strcpy(gdata->parm.szAuthor, val);
  227.                                         free(val);
  228.  
  229.                                         val = _ffx_read_str(&q);
  230.                                         strcpy(gdata->parm.szCopyright, val);
  231.                                         free(val);
  232.  
  233.                                         // Channels I, R, G, B, A
  234.                                         for (i = 0; i < 4; i++) {
  235.                                                 val = _ffx_read_str(&q);
  236.                                                 if (i == 0) {
  237.                                                         char* val2 = _ffx_read_str(&q);
  238.                                                         if (strcmp(val, "0") != 0) {
  239.                                                                 // "Intro channel" existing
  240.                                                                 // C++ wrong warning: Using uninitialized memory "val2" (C6001)
  241.                                                                 #pragma warning(suppress : 6001)
  242.                                                                 char* combined = (char*)malloc(strlen(val) + strlen(",") + strlen(val2) + 1);
  243.                                                                 if (combined != NULL) {
  244.                                                                         sprintf(combined, "%s,%s", val, val2);
  245.                                                                         free(val);
  246.                                                                         free(val2);
  247.                                                                         val = combined;
  248.                                                                 }
  249.                                                         }
  250.                                                         else {
  251.                                                                 free(val);
  252.                                                                 val = val2;
  253.                                                         }
  254.                                                 }
  255.                                                 if (strlen(val) >= sizeof(gdata->parm.szFormula[i])) {
  256.                                                         if (i == 0) {
  257.                                                                 simplealert(_strdup("Attention! The formula for channel I/R was too long (longer than 1023 characters) and was truncated."));
  258.                                                         }
  259.                                                         else if (i == 1) {
  260.                                                                 simplealert(_strdup("Attention! The formula for channel G was too long (longer than 1023 characters) and was truncated."));
  261.                                                         }
  262.                                                         else if (i == 2) {
  263.                                                                 simplealert(_strdup("Attention! The formula for channel B was too long (longer than 1023 characters) and was truncated."));
  264.                                                         }
  265.                                                         else if (i == 3) {
  266.                                                                 simplealert(_strdup("Attention! The formula for channel A was too long (longer than 1023 characters) and was truncated."));
  267.                                                         }
  268.                                                         // C++ wrong warning: Buffer overflow (C6386)
  269.                                                         #pragma warning(suppress : 6386)
  270.                                                         val[sizeof(gdata->parm.szFormula[i]) - 1] = '\0';
  271.                                                 }
  272.                                                 if (expr[i]) free(expr[i]);
  273.                                                 expr[i] = my_strdup(val);
  274.                                                 strcpy(gdata->parm.szFormula[i], val);
  275.                                                 free(val);
  276.                                         }
  277.  
  278.                                         // Sliders
  279.                                         for (i = 0; i < 8; i++) {
  280.                                                 char* sliderName;
  281.                                                 val = _ffx_read_str(&q);
  282.                                                 sliderName = val;
  283.                                                 if (format_version >= 12) {
  284.                                                         // Format FFX1.2 has prefixes {S} = Slider, {C} = Checkbox, none = Slider
  285.                                                         if ((sliderName[0] == '{') && (sliderName[1] == 'S') && (sliderName[2] == '}')) sliderName += 3;
  286.                                                         else if ((sliderName[0] == '{') && (sliderName[1] == 'C') && (sliderName[2] == '}')) sliderName += 3;
  287.                                                 }
  288.                                                 strcpy(gdata->parm.szCtl[i], sliderName);
  289.                                                 free(val);
  290.                                                 gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q);
  291.                                                 q += sizeof(byte);
  292.                                                 gdata->parm.val[i] = *((uint32_t*)q);
  293.                                                 slider[i] = *((uint32_t*)q);
  294.                                                 q += sizeof(uint32_t);
  295.                                         }
  296.  
  297.                                         // Maps (are not part of the format!)
  298.                                         strcpy(gdata->parm.szMap[0], "Map 0:");
  299.                                         strcpy(gdata->parm.szMap[1], "Map 1:");
  300.                                         strcpy(gdata->parm.szMap[2], "Map 2:");
  301.                                         strcpy(gdata->parm.szMap[3], "Map 3:");
  302.  
  303.                                         res = true;
  304.                                 }
  305.                         }
  306.                         PIDISPOSEHANDLE(h);
  307.                 }
  308.                 FSClose(refnum);
  309.         }
  310.  
  311.         if (res) gdata->obfusc = false;
  312.         return res;
  313. }
  314.  
  315. Boolean readfile_8bf(StandardFileReply *sfr,char **reason){
  316.         unsigned char magic[2];
  317.         FILECOUNT count;
  318.         Handle h;
  319.         Boolean res = false;
  320.         FILEREF refnum;
  321.  
  322.         if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){
  323.                 // check DOS EXE magic number
  324.                 count = 2;
  325.                 if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){
  326.                         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)
  327.                                 if( (h = readfileintohandle(refnum)) ){
  328.                                         long *q = (long*)PILOCKHANDLE(h,false);
  329.  
  330.                                         // look for signature at start of valid PARM resource
  331.                                         // This signature is observed in Filter Factory standalones.
  332.                                         for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
  333.                                         {
  334.                                                 res = readPARM(&gdata->parm, (Ptr)q);
  335.                                                 if (res) break;
  336.                                         }
  337.  
  338.                                         PIDISPOSEHANDLE(h);
  339.                                 }
  340.                         }
  341.                 } // else no point in proceeding
  342.                 FSClose(refnum);
  343.         }else
  344.                 *reason = _strdup("Could not open file.");
  345.  
  346.         if (res) gdata->obfusc = false;
  347.         return res;
  348. }
  349.  
  350. Boolean readPARM(PARM_T* pparm, Ptr p){
  351.         int i;
  352.         Boolean towin, tomac, fromwin, frommac;
  353.         unsigned int signature = *((unsigned int*)p);
  354.         unsigned int standalone = *((unsigned int*)p+1);
  355.  
  356.         // Find out our OS ("reader") the OS of the plugin ("source")
  357.         #ifdef MAC_ENV
  358.         towin = false;
  359.         tomac = true;
  360.         fromwin = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
  361.                 (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
  362.                 (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
  363.         frommac = ((signature == PARM_SIZE) ||
  364.                 (signature == PARM_SIZE_PREMIERE) ||
  365.                 (signature == PARM_SIG_MAC)) && standalone == 1;
  366.         #else
  367.         towin = true;
  368.         tomac = false;
  369.         fromwin = ((signature == PARM_SIZE) ||
  370.                 (signature == PARM_SIZE_PREMIERE) ||
  371.                 (signature == PARM_SIG_MAC)) && standalone == 1;
  372.         frommac = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
  373.                 (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
  374.                 (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
  375.         #endif
  376.  
  377.         // Is it a valid signature?
  378.         if (!fromwin && !frommac) {
  379.                 // No valid signature found
  380.                 return false;
  381.         }
  382.  
  383.         // Does it come from Premiere or Photoshop?
  384.         // Initialize pparm
  385.         if ((signature == PARM_SIZE_PREMIERE) || (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE)) {
  386.                 // It comes from Premiere. Swap R and B channel and convert to a Photoshop PARM_T
  387.                 convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
  388.         } else {
  389.                 // It is already Photoshop. Just copy to pparm.
  390.                 memcpy(pparm, p, sizeof(PARM_T));
  391.         }
  392.  
  393.         // Do we need to do string conversion?
  394.         if (frommac) {
  395.                 /* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */
  396.                 myp2cstr((unsigned char*)pparm->szCategory);
  397.                 myp2cstr((unsigned char*)pparm->szTitle);
  398.                 myp2cstr((unsigned char*)pparm->szCopyright);
  399.                 myp2cstr((unsigned char*)pparm->szAuthor);
  400.                 for (i = 0; i < 4; ++i)
  401.                         myp2cstr((unsigned char*)pparm->szMap[i]);
  402.                 for (i = 0; i < 8; ++i)
  403.                         myp2cstr((unsigned char*)pparm->szCtl[i]);
  404.         }
  405.  
  406.         // Case #1: Mac is reading Windows (Win16/32/64) plugin
  407.         if (fromwin && tomac) {
  408.                 size_t i;
  409.  
  410.                 // Convert copyright CRLF to CR (actually, just removing LF)
  411.                 char copyrightCRLF[256];
  412.                 char* p = &copyrightCRLF[0];
  413.                 for (i = 0; i < strlen(pparm->szCopyright); i++) {
  414.                         if (pparm->szCopyright[i] != LF) {
  415.                                 *p++ = pparm->szCopyright[i];
  416.                         }
  417.                 }
  418.                 *p++ = '\0';
  419.                 strcpy(pparm->szCopyright, copyrightCRLF);
  420.  
  421.                 // these are the only numeric fields we *have* to swap
  422.                 // all the rest are bool_t flags which (if we're careful) will work in either ordering
  423.                 for (i = 0; i < 8; ++i)
  424.                         pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
  425.         }
  426.  
  427.         // Case #2: Mac is reading Mac (in case the normal resource extraction didn't work)
  428.         // Nothing to do
  429.  
  430.         // 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)
  431.         // Nothing to do
  432.  
  433.         // Case #4: Windows is reading an old FilterFactory Mac file
  434.         // Note: You must read the ".rsrc" resource fork, not the standalone binary!
  435.         if (frommac && towin) {
  436.                 size_t i;
  437.  
  438.                 // Convert CR in the copyright field to CRLF.
  439.                 char copyrightCRLF[256];
  440.                 char* p = &copyrightCRLF[0];
  441.                 for (i = 0; i < strlen(pparm->szCopyright); i++) {
  442.                         *p++ = pparm->szCopyright[i];
  443.                         if (pparm->szCopyright[i] == CR) {
  444.                                 *p++ = LF;
  445.                         }
  446.                 }
  447.                 *p++ = '\0';
  448.                 strcpy(pparm->szCopyright, copyrightCRLF);
  449.  
  450.                 // these are the only numeric fields we *have* to swap
  451.                 // all the rest are bool_t flags which (if we're careful) will work in either ordering
  452.                 for (i = 0; i < 8; ++i)
  453.                         pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
  454.         }
  455.  
  456.         // Now set the values in pparm into the working variables expr[] and slider[], so that they are visible in the GUI
  457.  
  458.         for(i = 0; i < 4; ++i){
  459.                 if(expr[i]) free(expr[i]);
  460.                 expr[i] = my_strdup(pparm->szFormula[i]);
  461.         }
  462.  
  463.         for (i = 0; i < 8; ++i) {
  464.                 slider[i] = (uint8_t)pparm->val[i];
  465.                 /*
  466.                 if (slider[i] > 0xFF) {
  467.                         // Wrong endianess (e.g. reading a Mac rsrc on Windows)
  468.                         // Should not happen since we did the stuff above
  469.                         slider[i] = (uint8_t)EndianS32_LtoN(slider[i]);
  470.                 }
  471.                 */
  472.         }
  473.  
  474.         return true;
  475. }
  476.  
  477. Handle readfileintohandle(FILEREF r){
  478.         FILEPOS n;
  479.         Handle h;
  480.         Ptr p;
  481.  
  482.         if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){
  483.                 p = PILOCKHANDLE(h,false);
  484.                 if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){
  485.                         PIUNLOCKHANDLE(h);
  486.                         return h;
  487.                 }
  488.                 PIDISPOSEHANDLE(h);
  489.         }
  490.         return NULL;
  491. }
  492.  
  493. Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) {
  494.         size_t i;
  495.         for (i = 0; i < strlen(line); i++) {
  496.                 if (line[i] == '?') break; // avoid that "a?b:c" is detected as key
  497.                 if (line[i] == ':') {
  498.                         // Note: We are ignoring whitespaces, i.e. " A :" != "A:" (TODO: should we change this?)
  499.                         if ((searchkey == NULL) || ((i == strlen(searchkey)) && (memcmp(line, searchkey, i) == 0))) {
  500.                                 i++; // jump over ':' char
  501.                                 //while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left
  502.                                 *content = line + i;
  503.                                 return true;
  504.                         }
  505.                 }
  506.         }
  507.         *content = line;
  508.         return false;
  509. }
  510.  
  511. void _ffdcomp_removebrackets(char* x, char* maxptr) {
  512.         char* closingBracketPos = NULL;
  513.         Boolean openingBracketFound = false;
  514.         if (x[0] == '[') {
  515.                 openingBracketFound = true;
  516.         }
  517.         x[0] = ':';
  518.         x++;
  519.         while (x < maxptr) {
  520.                 if ((!openingBracketFound) && (x[0] == '[')) {
  521.                         openingBracketFound = true;
  522.                         x[0] = ' ';
  523.                 }
  524.                 else if (openingBracketFound) {
  525.                         if (x[0] == ']') {
  526.                                 closingBracketPos = x;
  527.                         }
  528.                         else if ((x[0] == CR) || (x[0] == LF)) {
  529.                                 if (closingBracketPos) closingBracketPos[0] = ' '; // last closing pos before CR/LF
  530.                                 break;
  531.                         }
  532.                 }
  533.                 x++;
  534.         }
  535. }
  536.  
  537. // isFormula=false => outputFile is C string. TXT linebreaks become spaces.
  538. // isFormula=true  => outputFile is C string. TXT line breaks become CRLF line breaks
  539. Boolean _picoReadProperty(char* inputFile, int maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) {
  540.         int i;
  541.         char* outputwork;
  542.         char* sline;
  543.         char* svalue;
  544.         char* inputwork;
  545.         char* inputworkinitial;
  546.         outputwork = outputFile;
  547.         sline = NULL;
  548.         svalue = NULL;
  549.         // Check parameters
  550.         if (maxOutput == 0) return false;
  551.         if (inputFile == 0) return false;
  552.         // Let input memory be read-only, +1 for terminal zero
  553.         //char* inputwork = inputFile;
  554.         inputwork = (char*)malloc(maxInput + 1);
  555.         inputworkinitial = inputwork;
  556.         if (inputwork == 0) return false;
  557.         memcpy(inputwork, inputFile, maxInput);
  558.         inputwork[maxInput] = 0; // otherwise strstr() will crash
  559.  
  560.         // Transform "FFDecomp" TXT file into the similar "PluginCommander" TXT file
  561.         if (strstr(inputwork, "Filter Factory Plugin Information:")) {
  562.                 char* x;
  563.                 char* k1;
  564.                 char* k2;
  565.                 // Metadata:
  566.                 x = strstr(inputwork, "CATEGORY:");
  567.                 if (x) memcpy(x, "Category:", strlen("Category:"));
  568.                 x = strstr(inputwork, "TITLE:");
  569.                 if (x) memcpy(x, "Title:", strlen("Title:"));
  570.                 x = strstr(inputwork, "COPYRIGHT:");
  571.                 if (x) memcpy(x, "Copyright:", strlen("Copyright:"));
  572.                 x = strstr(inputwork, "AUTHOR:");
  573.                 if (x) memcpy(x, "Author:", strlen("Author:"));
  574.                 // Controls:
  575.                 for (i = 0; i < 8; i++) {
  576.                         k1 = (char*)malloc(strlen("Control X:") + 1);
  577.                         sprintf(k1, "Control %d:", i);
  578.                         x = strstr(inputwork, k1);
  579.                         if (x) {
  580.                                 k2 = (char*)malloc(strlen("ctl[X]:   ") + 1);
  581.                                 sprintf(k2, "ctl[%d]:   ", i);
  582.                                 memcpy(x, k2, strlen(k2));
  583.                                 x += strlen("ctl[X]");
  584.                                 _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
  585.                                 free(k2);
  586.                         }
  587.                         free(k1);
  588.                 }
  589.                 // Maps:
  590.                 for (i = 0; i < 4; i++) {
  591.                         k1 = (char*)malloc(strlen("Map X:") + 1);
  592.                         sprintf(k1, "Map %d:", i);
  593.                         x = strstr(inputwork, k1);
  594.                         if (x) {
  595.                                 k2 = (char*)malloc(strlen("map[X]:") + 1);
  596.                                 sprintf(k2, "map[%d]:", i);
  597.                                 memcpy(x, k2, strlen(k2));
  598.                                 x += strlen("map[X]");
  599.                                 _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
  600.                                 free(k2);
  601.                         }
  602.                         free(k1);
  603.                 }
  604.                 // Convert all '\r' to '\n' for the next step to be easier
  605.                 for (i = 0; i < maxInput; i++) {
  606.                         if (inputworkinitial[i] == CR) inputworkinitial[i] = LF;
  607.                 }
  608.                 x = strstr(inputwork, "\nR=\n");
  609.                 if (x) memcpy(x, "\nR:\n", strlen("\nR:\n"));
  610.                 x = strstr(inputwork, "\nG=\n");
  611.                 if (x) memcpy(x, "\nG:\n", strlen("\nG:\n"));
  612.                 x = strstr(inputwork, "\nB=\n");
  613.                 if (x) memcpy(x, "\nB:\n", strlen("\nB:\n"));
  614.                 x = strstr(inputwork, "\nA=\n");
  615.                 if (x) memcpy(x, "\nA:\n", strlen("\nA:\n"));
  616.         }
  617.         // Replace all \r and \n with \0, so that we can parse easier
  618.         for (i = 0; i < maxInput; i++) {
  619.                 if (inputworkinitial[i] == CR) inputworkinitial[i] = 0;
  620.                 else if (inputworkinitial[i] == LF) inputworkinitial[i] = 0;
  621.         }
  622.  
  623.         // Find line that contains out key
  624.         inputwork = inputworkinitial;
  625.         do {
  626.                 if (inputwork > inputworkinitial + maxInput) {
  627.                         // Key not found. Set output to empty string
  628.                         outputwork[0] = 0;
  629.                         free(inputworkinitial);
  630.                         return false;
  631.                 }
  632.                 sline = inputwork;
  633.                 inputwork += strlen(sline) + 1;
  634.                 if (inputwork - 1 > inputworkinitial + maxInput) {
  635.                         // Key not found. Set output to empty string
  636.                         // TODO: will that be ever called?
  637.                         outputwork[0] = 0;
  638.                         free(inputworkinitial);
  639.                         return false;
  640.                 }
  641.         } while (!_picoLineContainsKey(sline, &svalue, property));
  642.  
  643.         // Read line(s) until we find a line with another key, or the line end
  644.         do {
  645.                 while ((svalue[0] == ' ') || (svalue[0] == TAB)) svalue++; // Trim left
  646.                 while ((svalue[strlen(svalue) - 1] == ' ') || (svalue[strlen(svalue) - 1] == TAB)) svalue[strlen(svalue) - 1] = 0; // Trim right
  647.  
  648.                 if (strlen(svalue) > 0) {
  649.                         if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) {
  650.                                 int remaining = maxOutput - (outputwork - outputFile) - 1;
  651.                                 //printf("BUFFER FULL (remaining = %d)\n", remaining);
  652.                                 memcpy(outputwork, svalue, remaining);
  653.                                 outputwork += remaining;
  654.                                 outputwork[0] = 0;
  655.                                 free(inputworkinitial);
  656.                                 return true;
  657.                         }
  658.                         else {
  659.                                 memcpy(outputwork, svalue, strlen(svalue));
  660.                                 outputwork += strlen(svalue);
  661.                                 if (isFormula) {
  662.                                         // Formulas: TXT line break stays line break (important if you have comments!)
  663.                                         outputwork[0] = CR;
  664.                                         outputwork[1] = LF;
  665.                                         outputwork += 2;
  666.                                 }
  667.                                 else {
  668.                                         // Everything else: TXT line breaks becomes single whitespace
  669.                                         outputwork[0] = ' ';
  670.                                         outputwork++;
  671.                                 }
  672.                         }
  673.                 }
  674.                 outputwork[0] = 0;
  675.  
  676.                 // Process next line
  677.                 if (inputwork > inputworkinitial + maxInput) break;
  678.                 sline = inputwork;
  679.                 inputwork += strlen(sline) + 1;
  680.                 if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called?
  681.         } while (!_picoLineContainsKey(sline, &svalue, NULL));
  682.  
  683.         // Remove trailing whitespace
  684.         if (outputwork > outputFile) {
  685.                 outputwork -= 1;
  686.                 outputwork[0] = 0;
  687.         }
  688.         free(inputworkinitial);
  689.         return true;
  690. }
  691.  
  692. Boolean readfile_picotxt(StandardFileReply* sfr, char** reason) {
  693.         extern int ctls[], maps[];
  694.  
  695.         Handle h;
  696.         Boolean res = false;
  697.         FILEREF refnum;
  698.  
  699.         if (!fileHasExtension(sfr, ".txt")) return false;
  700.  
  701.         if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
  702.                 if ((h = readfileintohandle(refnum))) {
  703.                         FILECOUNT count = PIGETHANDLESIZE(h);
  704.                         char* q = PILOCKHANDLE(h, false);
  705.  
  706.                         char out[256];
  707.                         if (_picoReadProperty(q, count, "Title", out, sizeof(out), false)) {
  708.                                 int i;
  709.  
  710.                                 // Plugin infos
  711.                                 _picoReadProperty(q, count, "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle), false);
  712.                                 _picoReadProperty(q, count, "Category", gdata->parm.szCategory, sizeof(gdata->parm.szCategory), false);
  713.                                 _picoReadProperty(q, count, "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor), false);
  714.                                 _picoReadProperty(q, count, "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright), false);
  715.                                 //_picoReadProperty(q, count, "Filename", gdata->parm.xxx, sizeof(gdata->parm.xxx), false);
  716.  
  717.                                 // Expressions
  718.                                 if (!_picoReadProperty(q, count, "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]), true))
  719.                                         strcpy(gdata->parm.szFormula[0], "r");
  720.                                 if (!_picoReadProperty(q, count, "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]), true))
  721.                                         strcpy(gdata->parm.szFormula[1], "g");
  722.                                 if (!_picoReadProperty(q, count, "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]), true))
  723.                                         strcpy(gdata->parm.szFormula[2], "b");
  724.                                 if (!_picoReadProperty(q, count, "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]), true))
  725.                                         strcpy(gdata->parm.szFormula[3], "a");
  726.                                 for (i = 0; i < 4; i++) {
  727.                                         if (expr[i]) free(expr[i]);
  728.                                         expr[i] = my_strdup(gdata->parm.szFormula[i]);
  729.                                 }
  730.  
  731.                                 // Slider names
  732.                                 for (i = 0; i < 8; i++) {
  733.                                         char keyname[7];
  734.                                         sprintf(keyname, "ctl[%d]", i);
  735.                                         _picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false);
  736.                                 }
  737.  
  738.                                 // Slider values
  739.                                 for (i = 0; i < 8; i++) {
  740.                                         char keyname[7], tmp[5];
  741.                                         sprintf(keyname, "val[%d]", i);
  742.                                         if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
  743.                                                 sprintf(keyname, "def[%d]", i);
  744.                                                 if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
  745.                                                         strcpy(tmp,"0");
  746.                                                 }
  747.                                         }
  748.                                         gdata->parm.val[i] = slider[i] = atoi(tmp);
  749.                                 }
  750.  
  751.                                 // Map names
  752.                                 for (i = 0; i < 4; i++) {
  753.                                         char keyname[7];
  754.                                         sprintf(keyname, "map[%d]", i);
  755.                                         _picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false);
  756.                                 }
  757.  
  758.                                 //These will be set when the expressions are evaluated anyway. So this part is optional:
  759.                                 checksliders(4, ctls, maps);
  760.                                 for (i = 0; i < 8; i++) gdata->parm.ctl_used[i] = ctls[i];
  761.                                 for (i = 0; i < 4; i++) gdata->parm.map_used[i] = maps[i];
  762.  
  763.                                 res = true;
  764.                         }
  765.  
  766.                         PIUNLOCKHANDLE(h);
  767.                         PIDISPOSEHANDLE(h);
  768.                 }
  769.                 FSClose(refnum);
  770.         }
  771.  
  772.         return res;
  773. }
  774.  
  775. Boolean readfile_afs_pff(StandardFileReply *sfr,char **reason){
  776.         FILEREF r;
  777.         Handle h;
  778.         Boolean res = false;
  779.  
  780.         if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){
  781.                 if( (h = readfileintohandle(r)) ){
  782.                         if( (res = readparams_afs_pff(h,reason)) ) {
  783.                                 gdata->standalone = false; // so metadata fields will default, if user chooses Make...
  784.  
  785.                                 if (fileHasExtension(sfr, ".pff")) {
  786.                                         // If it is a Premiere settings file, we need to swap the channels red and blue
  787.                                         // We just swap the pointers!
  788.                                         char* tmp;
  789.                                         tmp = expr[0];
  790.                                         expr[0] = expr[2];
  791.                                         expr[2] = tmp;
  792.                                 }
  793.                         }
  794.  
  795.                         PIDISPOSEHANDLE(h);
  796.                 }
  797.                 FSClose(r);
  798.         }else
  799.                 *reason = _strdup("Could not open the file.");
  800.  
  801.         return res;
  802. }
  803.