Subversion Repositories filter_foundry

Rev

Rev 505 | Rev 536 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
256 daniel-mar 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
503 daniel-mar 4
    Copyright (C) 2018-2022 Daniel Marschall, ViaThinkSoft
256 daniel-mar 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) {
272 daniel-mar 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
256 daniel-mar 32
               ((num<<24)&0xff000000); // byte 0 to byte 3
33
}
34
#endif
35
 
518 daniel-mar 36
#define BUFSIZE 4L<<10
37
#define MAXLINE 0x200
256 daniel-mar 38
 
492 daniel-mar 39
Boolean readparams_afs_pff(Handle h, TCHAR**reason){
256 daniel-mar 40
        Boolean res = false;
454 daniel-mar 41
        char linebuf[MAXLINE + 1] = { 0 };
42
        char curexpr[MAXEXPR + 1] = { 0 };
43
        char *p, *dataend, *q;
256 daniel-mar 44
        char c;
45
        int linecnt, lineptr, exprcnt;
46
 
482 daniel-mar 47
        if (!h) return false;
256 daniel-mar 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
 
421 daniel-mar 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)
256 daniel-mar 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")){
498 daniel-mar 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.
256 daniel-mar 72
                                        break;
73
                                }
74
                        }else if(linecnt<=8){
453 daniel-mar 75
                                int v;
76
                                v = atoi(linebuf);
77
                                if (v < 0) v = 0;
456 daniel-mar 78
                                else if (v > 255) v = 255;
79
                                slider[linecnt-1] = (uint8_t)v;
256 daniel-mar 80
                        }else{
81
                                if(lineptr){
82
                                        /* it's not an empty line; append it to current expr string */
83
                                        if( q+lineptr > curexpr+MAXEXPR ){
492 daniel-mar 84
                                                // TODO: isn't the limit 1024?! (because we need to have the NUL too?)
498 daniel-mar 85
                                                if (reason) *reason = FF_GetMsg_Cpy(MSG_EXPRESSION1024_FOUND_ID);
256 daniel-mar 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))){
498 daniel-mar 95
                                                if (reason) *reason = FF_GetMsg_Cpy(MSG_EXPRESSION_OOM_ID);
256 daniel-mar 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':
268 daniel-mar 117
                                                #if WIN_ENV
118
                                                c = CR;
119
                                                if (lineptr < MAXLINE)
274 daniel-mar 120
                                                        linebuf[lineptr++] = c;
268 daniel-mar 121
                                                c = LF;
122
                                                #else
123
                                                c = CR;
124
                                                #endif
125
                                                break;
256 daniel-mar 126
                                        case '\\': break;
408 daniel-mar 127
                                        //default:
492 daniel-mar 128
                                        //      if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("Unknown escape sequence in input.")); // TODO (Not so important): TRANSLATE
256 daniel-mar 129
                                        }
492 daniel-mar 130
                                }//else if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("truncated escape sequence ends input")); // TODO (Not so important): TRANSLATE
256 daniel-mar 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)
272 daniel-mar 149
                photoshop->val[i] = premiere->val[i];
256 daniel-mar 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)
272 daniel-mar 155
                photoshop->map_used[i] = premiere->map_used[i];
256 daniel-mar 156
        for (i=0;i<8;++i)
272 daniel-mar 157
                photoshop->ctl_used[i] = premiere->ctl_used[i];
393 daniel-mar 158
        sprintf(photoshop->szCategory, "Filter Factory"); // Premiere plugins do not have a category attribute
256 daniel-mar 159
        photoshop->iProtected = 0; // Premiere plugins do not have a protect flag
393 daniel-mar 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));
256 daniel-mar 163
        for (i=0;i<4;++i)
393 daniel-mar 164
                memcpy((void*)photoshop->szMap[i], (void*)premiere->szMap[i], sizeof(photoshop->szMap[i]));
256 daniel-mar 165
        for (i=0;i<8;++i)
393 daniel-mar 166
                memcpy((void*)photoshop->szCtl[i], (void*)premiere->szCtl[i], sizeof(photoshop->szCtl[i]));
256 daniel-mar 167
 
168
        if (premiere->singleExpression) {
393 daniel-mar 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]));
256 daniel-mar 173
        } else {
393 daniel-mar 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]));
256 daniel-mar 178
        }
179
}
180
 
366 daniel-mar 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);
454 daniel-mar 187
        val = (char*)malloc((size_t)len + 1);
366 daniel-mar 188
        if (val != NULL) {
189
                memcpy(val, (char*)*q, len);
190
                val[len] = 0;
191
        }
192
        *q += len;
193
        return val;
194
}
195
 
492 daniel-mar 196
Boolean readfile_ffx(StandardFileReply* sfr, TCHAR** reason) {
366 daniel-mar 197
        Handle h;
198
        Boolean res = false;
199
        FILEREF refnum;
200
        uint32_t len;
201
        char* val;
367 daniel-mar 202
        int format_version = -1;
366 daniel-mar 203
        int i;
204
 
433 daniel-mar 205
        UNREFERENCED_PARAMETER(reason);
206
 
366 daniel-mar 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);
367 daniel-mar 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;
366 daniel-mar 217
                                free(val);
367 daniel-mar 218
                                if (format_version > 0) {
492 daniel-mar 219
                                        simplewarning_id(MSG_FILTERS_UNLIMITED_WARNING_ID);
368 daniel-mar 220
 
366 daniel-mar 221
                                        val = _ffx_read_str(&q);
393 daniel-mar 222
                                        strcpy(gdata->parm.szTitle, val);
366 daniel-mar 223
                                        free(val);
224
 
225
                                        val = _ffx_read_str(&q);
393 daniel-mar 226
                                        strcpy(gdata->parm.szCategory, val);
366 daniel-mar 227
                                        free(val);
228
 
229
                                        val = _ffx_read_str(&q);
393 daniel-mar 230
                                        strcpy(gdata->parm.szAuthor, val);
366 daniel-mar 231
                                        free(val);
232
 
233
                                        val = _ffx_read_str(&q);
393 daniel-mar 234
                                        strcpy(gdata->parm.szCopyright, val);
366 daniel-mar 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) {
370 daniel-mar 241
                                                        char* val2 = _ffx_read_str(&q);
366 daniel-mar 242
                                                        if (strcmp(val, "0") != 0) {
243
                                                                // "Intro channel" existing
370 daniel-mar 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
                                                                }
366 daniel-mar 253
                                                        }
254
                                                        else {
255
                                                                free(val);
370 daniel-mar 256
                                                                val = val2;
366 daniel-mar 257
                                                        }
258
                                                }
393 daniel-mar 259
                                                if (strlen(val) >= sizeof(gdata->parm.szFormula[i])) {
368 daniel-mar 260
                                                        if (i == 0) {
492 daniel-mar 261
                                                                simplealert_id(MSG_FORMULA_IR_1023_TRUNCATED_ID);
368 daniel-mar 262
                                                        }
263
                                                        else if (i == 1) {
492 daniel-mar 264
                                                                simplealert_id(MSG_FORMULA_G_1023_TRUNCATED_ID);
368 daniel-mar 265
                                                        }
266
                                                        else if (i == 2) {
492 daniel-mar 267
                                                                simplealert_id(MSG_FORMULA_B_1023_TRUNCATED_ID);
368 daniel-mar 268
                                                        }
269
                                                        else if (i == 3) {
492 daniel-mar 270
                                                                simplealert_id(MSG_FORMULA_A_1023_TRUNCATED_ID);
368 daniel-mar 271
                                                        }
370 daniel-mar 272
                                                        // C++ wrong warning: Buffer overflow (C6386)
273
                                                        #pragma warning(suppress : 6386)
393 daniel-mar 274
                                                        val[sizeof(gdata->parm.szFormula[i]) - 1] = '\0';
368 daniel-mar 275
                                                }
395 daniel-mar 276
                                                if (expr[i]) free(expr[i]);
366 daniel-mar 277
                                                expr[i] = my_strdup(val);
393 daniel-mar 278
                                                strcpy(gdata->parm.szFormula[i], val);
366 daniel-mar 279
                                                free(val);
280
                                        }
281
 
282
                                        // Sliders
283
                                        for (i = 0; i < 8; i++) {
367 daniel-mar 284
                                                char* sliderName;
453 daniel-mar 285
                                                int v;
366 daniel-mar 286
                                                val = _ffx_read_str(&q);
367 daniel-mar 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
                                                }
393 daniel-mar 293
                                                strcpy(gdata->parm.szCtl[i], sliderName);
366 daniel-mar 294
                                                free(val);
367 daniel-mar 295
                                                gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q);
366 daniel-mar 296
                                                q += sizeof(byte);
297
                                                gdata->parm.val[i] = *((uint32_t*)q);
453 daniel-mar 298
                                                v = *((uint32_t*)q);
299
                                                if (v < 0) v = 0;
456 daniel-mar 300
                                                else if (v > 255) v = 255;
301
                                                slider[i] = (uint8_t)v;
366 daniel-mar 302
                                                q += sizeof(uint32_t);
303
                                        }
304
 
390 daniel-mar 305
                                        // Maps (are not part of the format!)
393 daniel-mar 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:");
390 daniel-mar 310
 
366 daniel-mar 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
 
492 daniel-mar 323
Boolean readfile_8bf(StandardFileReply *sfr, TCHAR**reason){
256 daniel-mar 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'*/){
309 daniel-mar 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)
256 daniel-mar 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
                                        {
394 daniel-mar 342
                                                res = readPARM(&gdata->parm, (Ptr)q);
256 daniel-mar 343
                                                if (res) break;
344
                                        }
341 daniel-mar 345
 
256 daniel-mar 346
                                        PIDISPOSEHANDLE(h);
347
                                }
348
                        }
349
                } // else no point in proceeding
350
                FSClose(refnum);
351
        }else
498 daniel-mar 352
                if (reason) *reason = FF_GetMsg_Cpy(MSG_CANNOT_OPEN_FILE_ID);
256 daniel-mar 353
 
354
        if (res) gdata->obfusc = false;
355
        return res;
356
}
357
 
394 daniel-mar 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);
256 daniel-mar 362
 
394 daniel-mar 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
256 daniel-mar 394
                convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
395
        } else {
394 daniel-mar 396
                // It is already Photoshop. Just copy to pparm.
397
                memcpy(pparm, p, sizeof(PARM_T));
256 daniel-mar 398
        }
399
 
394 daniel-mar 400
        // Do we need to do string conversion?
401
        if (frommac) {
456 daniel-mar 402
                int i;
403
 
394 daniel-mar 404
                /* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */
393 daniel-mar 405
                myp2cstr((unsigned char*)pparm->szCategory);
406
                myp2cstr((unsigned char*)pparm->szTitle);
407
                myp2cstr((unsigned char*)pparm->szCopyright);
408
                myp2cstr((unsigned char*)pparm->szAuthor);
394 daniel-mar 409
                for (i = 0; i < 4; ++i)
393 daniel-mar 410
                        myp2cstr((unsigned char*)pparm->szMap[i]);
394 daniel-mar 411
                for (i = 0; i < 8; ++i)
393 daniel-mar 412
                        myp2cstr((unsigned char*)pparm->szCtl[i]);
256 daniel-mar 413
        }
414
 
394 daniel-mar 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)
454 daniel-mar 420
                char copyrightCRLF[256] = { 0 };
456 daniel-mar 421
                char* pCopyright = &copyrightCRLF[0];
394 daniel-mar 422
                for (i = 0; i < strlen(pparm->szCopyright); i++) {
423
                        if (pparm->szCopyright[i] != LF) {
456 daniel-mar 424
                                *pCopyright++ = pparm->szCopyright[i];
394 daniel-mar 425
                        }
426
                }
456 daniel-mar 427
                *pCopyright++ = '\0';
394 daniel-mar 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.
454 daniel-mar 448
                char copyrightCRLF[256] = { 0 };
456 daniel-mar 449
                char* pCopyright = &copyrightCRLF[0];
394 daniel-mar 450
                for (i = 0; i < strlen(pparm->szCopyright); i++) {
456 daniel-mar 451
                        *pCopyright++ = pparm->szCopyright[i];
394 daniel-mar 452
                        if (pparm->szCopyright[i] == CR) {
456 daniel-mar 453
                                *pCopyright++ = LF;
394 daniel-mar 454
                        }
455
                }
456 daniel-mar 456
                *pCopyright++ = '\0';
394 daniel-mar 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
456 daniel-mar 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
                }
394 daniel-mar 472
 
456 daniel-mar 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
                        */
339 daniel-mar 482
                }
483
        }
256 daniel-mar 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
 
385 daniel-mar 504
Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) {
386 daniel-mar 505
        size_t i;
506
        for (i = 0; i < strlen(line); i++) {
388 daniel-mar 507
                if (line[i] == '?') break; // avoid that "a?b:c" is detected as key
385 daniel-mar 508
                if (line[i] == ':') {
386 daniel-mar 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
387 daniel-mar 512
                                //while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left
385 daniel-mar 513
                                *content = line + i;
514
                                return true;
515
                        }
516
                }
517
        }
518
        *content = line;
519
        return false;
520
}
521
 
388 daniel-mar 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
 
393 daniel-mar 548
// isFormula=false => outputFile is C string. TXT linebreaks become spaces.
386 daniel-mar 549
// isFormula=true  => outputFile is C string. TXT line breaks become CRLF line breaks
505 daniel-mar 550
Boolean _picoReadProperty(char* inputFile, size_t maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) {
551
        size_t i;
386 daniel-mar 552
        char* outputwork;
553
        char* sline;
554
        char* svalue;
555
        char* inputwork;
556
        char* inputworkinitial;
557
        outputwork = outputFile;
558
        sline = NULL;
559
        svalue = NULL;
385 daniel-mar 560
        // Check parameters
386 daniel-mar 561
        if (maxOutput == 0) return false;
385 daniel-mar 562
        if (inputFile == 0) return false;
563
        // Let input memory be read-only, +1 for terminal zero
564
        //char* inputwork = inputFile;
454 daniel-mar 565
        inputwork = (char*)malloc((size_t)maxInput + 1);
386 daniel-mar 566
        inputworkinitial = inputwork;
385 daniel-mar 567
        if (inputwork == 0) return false;
568
        memcpy(inputwork, inputFile, maxInput);
388 daniel-mar 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:");
389 daniel-mar 578
                if (x) memcpy(x, "Category:", strlen("Category:"));
388 daniel-mar 579
                x = strstr(inputwork, "TITLE:");
389 daniel-mar 580
                if (x) memcpy(x, "Title:", strlen("Title:"));
388 daniel-mar 581
                x = strstr(inputwork, "COPYRIGHT:");
389 daniel-mar 582
                if (x) memcpy(x, "Copyright:", strlen("Copyright:"));
388 daniel-mar 583
                x = strstr(inputwork, "AUTHOR:");
389 daniel-mar 584
                if (x) memcpy(x, "Author:", strlen("Author:"));
388 daniel-mar 585
                // Controls:
586
                for (i = 0; i < 8; i++) {
587
                        k1 = (char*)malloc(strlen("Control X:") + 1);
505 daniel-mar 588
                        sprintf(k1, "Control %d:", (int)i);
388 daniel-mar 589
                        x = strstr(inputwork, k1);
590
                        if (x) {
591
                                k2 = (char*)malloc(strlen("ctl[X]:   ") + 1);
505 daniel-mar 592
                                sprintf(k2, "ctl[%d]:   ", (int)i);
389 daniel-mar 593
                                memcpy(x, k2, strlen(k2));
388 daniel-mar 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);
505 daniel-mar 603
                        sprintf(k1, "Map %d:", (int)i);
388 daniel-mar 604
                        x = strstr(inputwork, k1);
605
                        if (x) {
606
                                k2 = (char*)malloc(strlen("map[X]:") + 1);
505 daniel-mar 607
                                sprintf(k2, "map[%d]:", (int)i);
389 daniel-mar 608
                                memcpy(x, k2, strlen(k2));
388 daniel-mar 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");
389 daniel-mar 620
                if (x) memcpy(x, "\nR:\n", strlen("\nR:\n"));
388 daniel-mar 621
                x = strstr(inputwork, "\nG=\n");
389 daniel-mar 622
                if (x) memcpy(x, "\nG:\n", strlen("\nG:\n"));
388 daniel-mar 623
                x = strstr(inputwork, "\nB=\n");
389 daniel-mar 624
                if (x) memcpy(x, "\nB:\n", strlen("\nB:\n"));
388 daniel-mar 625
                x = strstr(inputwork, "\nA=\n");
389 daniel-mar 626
                if (x) memcpy(x, "\nA:\n", strlen("\nA:\n"));
388 daniel-mar 627
        }
385 daniel-mar 628
        // Replace all \r and \n with \0, so that we can parse easier
386 daniel-mar 629
        for (i = 0; i < maxInput; i++) {
388 daniel-mar 630
                if (inputworkinitial[i] == CR) inputworkinitial[i] = 0;
393 daniel-mar 631
                else if (inputworkinitial[i] == LF) inputworkinitial[i] = 0;
385 daniel-mar 632
        }
388 daniel-mar 633
 
385 daniel-mar 634
        // Find line that contains out key
388 daniel-mar 635
        inputwork = inputworkinitial;
385 daniel-mar 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));
388 daniel-mar 653
 
385 daniel-mar 654
        // Read line(s) until we find a line with another key, or the line end
655
        do {
387 daniel-mar 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
386 daniel-mar 658
 
385 daniel-mar 659
                if (strlen(svalue) > 0) {
393 daniel-mar 660
                        if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) {
444 daniel-mar 661
                                size_t remaining = maxOutput - (outputwork - outputFile) - 1;
385 daniel-mar 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);
386 daniel-mar 672
                                if (isFormula) {
673
                                        // Formulas: TXT line break stays line break (important if you have comments!)
388 daniel-mar 674
                                        outputwork[0] = CR;
675
                                        outputwork[1] = LF;
387 daniel-mar 676
                                        outputwork += 2;
386 daniel-mar 677
                                }
678
                                else {
679
                                        // Everything else: TXT line breaks becomes single whitespace
680
                                        outputwork[0] = ' ';
681
                                        outputwork++;
682
                                }
385 daniel-mar 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;
386 daniel-mar 691
                if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called?
385 daniel-mar 692
        } while (!_picoLineContainsKey(sline, &svalue, NULL));
388 daniel-mar 693
 
385 daniel-mar 694
        // Remove trailing whitespace
695
        if (outputwork > outputFile) {
696
                outputwork -= 1;
697
                outputwork[0] = 0;
698
        }
699
        free(inputworkinitial);
700
        return true;
701
}
702
 
492 daniel-mar 703
Boolean readfile_picotxt(StandardFileReply* sfr, TCHAR** reason) {
389 daniel-mar 704
        extern int ctls[], maps[];
705
 
385 daniel-mar 706
        Handle h;
707
        Boolean res = false;
708
        FILEREF refnum;
709
 
433 daniel-mar 710
        UNREFERENCED_PARAMETER(reason);
711
 
444 daniel-mar 712
        if (!fileHasExtension(sfr, TEXT(".txt"))) return false;
385 daniel-mar 713
 
714
        if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
715
                if ((h = readfileintohandle(refnum))) {
505 daniel-mar 716
                        FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h);
385 daniel-mar 717
                        char* q = PILOCKHANDLE(h, false);
718
 
719
                        char out[256];
720
                        if (_picoReadProperty(q, count, "Title", out, sizeof(out), false)) {
386 daniel-mar 721
                                int i;
385 daniel-mar 722
 
723
                                // Plugin infos
393 daniel-mar 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);
385 daniel-mar 729
 
730
                                // Expressions
393 daniel-mar 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");
386 daniel-mar 739
                                for (i = 0; i < 4; i++) {
385 daniel-mar 740
                                        if (expr[i]) free(expr[i]);
393 daniel-mar 741
                                        expr[i] = my_strdup(gdata->parm.szFormula[i]);
385 daniel-mar 742
                                }
743
 
744
                                // Slider names
386 daniel-mar 745
                                for (i = 0; i < 8; i++) {
385 daniel-mar 746
                                        char keyname[7];
747
                                        sprintf(keyname, "ctl[%d]", i);
393 daniel-mar 748
                                        _picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false);
385 daniel-mar 749
                                }
750
 
751
                                // Slider values
386 daniel-mar 752
                                for (i = 0; i < 8; i++) {
753
                                        char keyname[7], tmp[5];
453 daniel-mar 754
                                        int v;
385 daniel-mar 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)) {
393 daniel-mar 759
                                                        strcpy(tmp,"0");
385 daniel-mar 760
                                                }
761
                                        }
453 daniel-mar 762
                                        v = atoi(tmp);
763
                                        if (v < 0) v = 0;
456 daniel-mar 764
                                        else if (v > 255) v = 255;
765
                                        gdata->parm.val[i] = slider[i] = (uint8_t)v;
385 daniel-mar 766
                                }
767
 
768
                                // Map names
386 daniel-mar 769
                                for (i = 0; i < 4; i++) {
385 daniel-mar 770
                                        char keyname[7];
771
                                        sprintf(keyname, "map[%d]", i);
393 daniel-mar 772
                                        _picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false);
385 daniel-mar 773
                                }
774
 
389 daniel-mar 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];
385 daniel-mar 779
 
780
                                res = true;
781
                        }
782
 
783
                        PIUNLOCKHANDLE(h);
784
                        PIDISPOSEHANDLE(h);
785
                }
786
                FSClose(refnum);
787
        }
788
 
789
        return res;
790
}
791
 
492 daniel-mar 792
Boolean readfile_afs_pff(StandardFileReply *sfr, TCHAR**reason){
256 daniel-mar 793
        FILEREF r;
794
        Handle h;
795
        Boolean res = false;
796
 
797
        if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){
798
                if( (h = readfileintohandle(r)) ){
408 daniel-mar 799
                        if( (res = readparams_afs_pff(h,reason)) ) {
256 daniel-mar 800
                                gdata->standalone = false; // so metadata fields will default, if user chooses Make...
801
 
444 daniel-mar 802
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
256 daniel-mar 803
                                        // If it is a Premiere settings file, we need to swap the channels red and blue
263 daniel-mar 804
                                        // We just swap the pointers!
256 daniel-mar 805
                                        char* tmp;
263 daniel-mar 806
                                        tmp = expr[0];
807
                                        expr[0] = expr[2];
808
                                        expr[2] = tmp;
256 daniel-mar 809
                                }
810
                        }
811
 
812
                        PIDISPOSEHANDLE(h);
813
                }
814
                FSClose(r);
492 daniel-mar 815
        }
816
        else
498 daniel-mar 817
                if (reason) *reason = FF_GetMsg_Cpy(MSG_CANNOT_OPEN_FILE_ID);
256 daniel-mar 818
 
819
        return res;
820
}