Subversion Repositories filter_foundry

Rev

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