Subversion Repositories filter_foundry

Rev

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