-
Notifications
You must be signed in to change notification settings - Fork 1
/
read.c
1240 lines (1101 loc) · 41.3 KB
/
read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
Copyright (C) 2003-2009 Toby Thain, [email protected]
Copyright (C) 2018-2024 Daniel Marschall, ViaThinkSoft
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// TODO: Change Windows1252 methods to the current codepage of the system. Note that we should stay compatible with Windows 3.11 if possible
#include "ff.h"
#include "file_compat.h"
#ifdef MAC_ENV
#include <Endian.h>
#else
int EndianS32_LtoN(int num) {
return ((num>>24)&0xfful) + // move byte 3 to byte 0
((num<<8)&0xff0000ul) + // move byte 1 to byte 2
((num>>8)&0xff00ul) + // move byte 2 to byte 1
((num<<24)&0xff000000ul); // byte 0 to byte 3
}
#endif
#define BUFSIZE 4L<<10
#define MAXLINE 0x200
FFLoadingResult readparams_afs_pff(Handle h, Boolean premiereOrder){
FFLoadingResult res = FF_LOADING_RESULT(MSG_LOADFILE_UNKNOWN_FORMAT_ID);
char linebuf[MAXLINE] = { 0 };
char curexpr[MAXEXPR] = { 0 };
char *p, *dataend, *q;
char c;
int linecnt, lineptr, exprcnt;
//if (!h) return nilHandleErr;
p = PILOCKHANDLE(h,false);
dataend = p + PIGETHANDLESIZE(h);
q = curexpr;
linecnt = exprcnt = lineptr = 0;
while(p < dataend){
c = *p++;
if(c==CR || c==LF){ /* detected end of line */
/* look ahead to see if we need to skip a line feed (DOS CRLF EOL convention) */
if(p < dataend && c == CR && *p == LF)
++p;
linebuf[lineptr] = '\0'; /* add terminating NUL to line buffer */
/* process complete line */
if(linecnt==0){
if(strcmp(linebuf,"%RGB-1.0") != 0){
res = FF_LOADING_RESULT(MSG_INVALID_FILE_SIGNATURE_ID);
break;
}
} else if(linecnt<=8) {
int v;
v = atoi(linebuf);
if (v < 0) v = 0;
else if (v > 255) v = 255;
gdata->parm.val[linecnt-1] = (uint8_t)v;
} else {
if(lineptr){
/* it's not an empty line; append it to current expr string */
if( (q-curexpr) + lineptr >= MAXEXPR) {
res = FF_LOADING_RESULT(MSG_EXPRESSION1024_FOUND_ID);
break;
}
q = cat(q,linebuf);
} else {
/* it's an empty line: we've completed the expr string */
*q = '\0';
if (premiereOrder) {
// Premiere has the order BGRA, while Photoshop (and our internal order) is RGBA
if (exprcnt == 0) strcpy(gdata->parm.szFormula[2], curexpr);
else if (exprcnt == 2) strcpy(gdata->parm.szFormula[0], curexpr);
else strcpy(gdata->parm.szFormula[exprcnt], curexpr);
} else {
strcpy(gdata->parm.szFormula[exprcnt], curexpr);
}
if(++exprcnt == 4){
res = FF_LOADING_RESULT(LOADING_OK);
break; /* got everything we want */
}
q = curexpr; /* empty current expr, ready for next one */
}
}
++linecnt;
lineptr = 0;
} else {
/* store character */
if(c=='\\'){ /* escape sequence */
if(p < dataend){
c = *p++;
switch(c){
case 'r':
#if WIN_ENV
c = CR;
if (lineptr < MAXLINE-1)
linebuf[lineptr++] = c;
c = LF;
#else
c = CR;
#endif
break;
case '\\': break;
//default:
// if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("Unknown escape sequence in input.")); // TODO (Not so important): TRANSLATE
}
}//else if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("truncated escape sequence ends input")); // TODO (Not so important): TRANSLATE
}
if(lineptr < MAXLINE-1)
linebuf[lineptr++] = c;
}
}
PIUNLOCKHANDLE(h);
return res;
}
void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) {
int i;
photoshop->cbSize = sizeof(PARM_T);
photoshop->standalone = premiere->standalone;
for (i=0;i<8;++i)
photoshop->val[i] = premiere->val[i];
photoshop->popDialog = premiere->popDialog;
photoshop->unknown1 = premiere->unknown1;
photoshop->unknown2 = premiere->unknown2;
photoshop->unknown3 = premiere->unknown3;
for (i=0;i<4;++i)
photoshop->map_used[i] = premiere->map_used[i];
for (i=0;i<8;++i)
photoshop->ctl_used[i] = premiere->ctl_used[i];
sprintf(photoshop->szCategory, "Filter Factory"); // Premiere plugins do not have a category attribute
photoshop->iProtected = 0; // Premiere plugins do not have a protect flag
memcpy((void*)photoshop->szTitle, (void*)premiere->szTitle, sizeof(photoshop->szTitle));
memcpy((void*)photoshop->szCopyright, (void*)premiere->szCopyright, sizeof(photoshop->szCopyright));
memcpy((void*)photoshop->szAuthor, (void*)premiere->szAuthor, sizeof(photoshop->szAuthor));
for (i=0;i<4;++i)
memcpy((void*)photoshop->szMap[i], (void*)premiere->szMap[i], sizeof(photoshop->szMap[i]));
for (i=0;i<8;++i)
memcpy((void*)photoshop->szCtl[i], (void*)premiere->szCtl[i], sizeof(photoshop->szCtl[i]));
if (premiere->singleExpression) {
memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
} else {
memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[2], sizeof(photoshop->szFormula[2]));
memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[1], sizeof(photoshop->szFormula[1]));
memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[0], sizeof(photoshop->szFormula[0]));
memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
}
}
char* _ffx_read_str(char** q) {
uint32_t len;
char* val;
len = *((uint32_t*)*q);
*q += sizeof(uint32_t);
val = (char*)malloc((size_t)len + 1);
if (val != NULL) {
memcpy(val, (char*)*q, len);
val[len] = '\0';
}
*q += len;
return val;
}
FFLoadingResult readfile_ffx(StandardFileReply* sfr) {
Handle h;
FFLoadingResult res = FF_LOADING_RESULT(MSG_LOADFILE_UNKNOWN_FORMAT_ID);
FILEREF refnum;
uint32_t len;
char* val;
int format_version = -1;
int i;
if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
if ((h = readfileintohandle(refnum))) {
char* q = (char*)PILOCKHANDLE(h, false);
len = *((uint32_t*)q);
if (len != 6) {
res = FF_LOADING_RESULT(MSG_INVALID_FILE_SIGNATURE_ID);
} else {
val = _ffx_read_str(&q);
if (strcmp(val, "FFX1.0") == 0) format_version = 10;
else if (strcmp(val, "FFX1.1") == 0) format_version = 11;
else if (strcmp(val, "FFX1.2") == 0) format_version = 12;
free(val);
if (format_version < 0) {
res = FF_LOADING_RESULT(MSG_INVALID_FILE_SIGNATURE_ID);
} else {
simplewarning_id(MSG_FILTERS_UNLIMITED_WARNING_ID);
val = _ffx_read_str(&q);
if (strlen(val) >= sizeof(gdata->parm.szTitle)) {
val[sizeof(gdata->parm.szTitle) - 1] = '\0';
}
strcpy(gdata->parm.szTitle, val);
free(val);
val = _ffx_read_str(&q);
if (strlen(val) >= sizeof(gdata->parm.szCategory)) {
val[sizeof(gdata->parm.szCategory) - 1] = '\0';
}
strcpy(gdata->parm.szCategory, val);
free(val);
val = _ffx_read_str(&q);
if (strlen(val) >= sizeof(gdata->parm.szAuthor)) {
val[sizeof(gdata->parm.szAuthor) - 1] = '\0';
}
strcpy(gdata->parm.szAuthor, val);
free(val);
val = _ffx_read_str(&q);
if (strlen(val) >= sizeof(gdata->parm.szCopyright)) {
val[sizeof(gdata->parm.szCopyright) - 1] = '\0';
}
strcpy(gdata->parm.szCopyright, val);
free(val);
// Channels I, R, G, B, A
for (i = 0; i < 4; i++) {
val = _ffx_read_str(&q);
if (i == 0) {
char* val2 = _ffx_read_str(&q);
if (strcmp(val, "0") != 0) {
// "Intro channel" existing
// C++ wrong warning: Using uninitialized memory "val2" (C6001)
#pragma warning(suppress : 6001)
char* combined = (char*)malloc(strlen(val) + strlen(",") + strlen(val2) + 1/*NUL byte*/);
if (combined != NULL) {
sprintf(combined, "%s,%s", val, val2);
free(val);
free(val2);
val = combined;
}
} else {
free(val);
val = val2;
}
}
if (strlen(val) >= sizeof(gdata->parm.szFormula[i])) {
if (i == 0) {
simplealert_id(MSG_FORMULA_IR_1023_TRUNCATED_ID);
} else if (i == 1) {
simplealert_id(MSG_FORMULA_G_1023_TRUNCATED_ID);
} else if (i == 2) {
simplealert_id(MSG_FORMULA_B_1023_TRUNCATED_ID);
} else if (i == 3) {
simplealert_id(MSG_FORMULA_A_1023_TRUNCATED_ID);
}
// C++ wrong warning: Buffer overflow (C6386)
#pragma warning(suppress : 6386)
val[sizeof(gdata->parm.szFormula[i]) - 1] = '\0';
}
strcpy(gdata->parm.szFormula[i], val);
free(val);
}
// Sliders
for (i = 0; i < 8; i++) {
char* sliderName;
int v;
val = _ffx_read_str(&q);
sliderName = val;
if (format_version >= 12) {
// Format FFX1.2 has prefixes {S} = Slider, {C} = Checkbox, none = Slider
if ((sliderName[0] == '{') && (sliderName[1] == 'S') && (sliderName[2] == '}')) sliderName += 3;
else if ((sliderName[0] == '{') && (sliderName[1] == 'C') && (sliderName[2] == '}')) sliderName += 3;
}
if (strlen(val) >= sizeof(gdata->parm.szCtl[i])) {
val[sizeof(gdata->parm.szCtl[i]) - 1] = '\0';
}
strcpy(gdata->parm.szCtl[i], sliderName);
free(val);
gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q);
q += sizeof(byte);
gdata->parm.val[i] = *((uint32_t*)q);
v = *((uint32_t*)q);
if (v < 0) v = 0;
else if (v > 255) v = 255;
gdata->parm.val[i] = (uint8_t)v;
q += sizeof(uint32_t);
}
// Maps (are not part of the format!)
strcpy(gdata->parm.szMap[0], "Map 0:");
strcpy(gdata->parm.szMap[1], "Map 1:");
strcpy(gdata->parm.szMap[2], "Map 2:");
strcpy(gdata->parm.szMap[3], "Map 3:");
res = FF_LOADING_RESULT(LOADING_OK);
}
}
PIDISPOSEHANDLE(h);
}
FSClose(refnum);
}
if (res.msgid == LOADING_OK) gdata->obfusc = false;
return res;
}
FFLoadingResult readfile_8bf(StandardFileReply *sfr){
unsigned char magic[2];
FILECOUNT count;
Handle h;
FFLoadingResult res = FF_LOADING_RESULT(MSG_LOADFILE_UNKNOWN_FORMAT_ID);
FILEREF refnum;
if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){
// check DOS EXE magic number
count = 2;
if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){
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)
if( (h = readfileintohandle(refnum)) ){
long *q = (long*)PILOCKHANDLE(h,false);
// look for signature at start of valid PARM resource
// This signature is observed in Filter Factory standalones.
for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
{
res = readPARM(&gdata->parm, (Ptr)q);
if (res.msgid == LOADING_OK) break;
}
PIDISPOSEHANDLE(h);
}
}
} // else no point in proceeding
FSClose(refnum);
} else {
res = FF_LOADING_RESULT(MSG_CANNOT_OPEN_FILE_ID);
}
if (res.msgid == LOADING_OK) gdata->obfusc = false;
return res;
}
FFLoadingResult readPARM(PARM_T* pparm, Ptr p){
Boolean towin, tomac, fromwin, frommac;
unsigned int signature = *((unsigned int*)p);
unsigned int standalone = *((unsigned int*)p+1);
// Find out our OS ("reader") the OS of the plugin ("source")
#ifdef MAC_ENV
towin = false;
tomac = true;
fromwin = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
(EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
(EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
frommac = ((signature == PARM_SIZE) ||
(signature == PARM_SIZE_PREMIERE) ||
(signature == PARM_SIG_MAC)) && standalone == 1;
#else
towin = true;
tomac = false;
fromwin = ((signature == PARM_SIZE) ||
(signature == PARM_SIZE_PREMIERE) ||
(signature == PARM_SIG_MAC)) && standalone == 1;
frommac = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
(EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
(EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
#endif
// Is it a valid signature?
if (!fromwin && !frommac) {
// No valid signature found
return FF_LOADING_RESULT(MSG_INVALID_FILE_SIGNATURE_ID);
}
// Does it come from Premiere or Photoshop?
// Initialize pparm
if ((signature == PARM_SIZE_PREMIERE) || (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE)) {
// It comes from Premiere. Swap R and B channel and convert to a Photoshop PARM_T
convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
} else {
// It is already Photoshop. Just copy to pparm.
memcpy(pparm, p, sizeof(PARM_T));
}
// Do we need to do string conversion?
if (frommac) {
int i;
/* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */
myp2cstr((unsigned char*)pparm->szCategory);
myp2cstr((unsigned char*)pparm->szTitle);
myp2cstr((unsigned char*)pparm->szCopyright);
myp2cstr((unsigned char*)pparm->szAuthor);
for (i = 0; i < 4; ++i)
myp2cstr((unsigned char*)pparm->szMap[i]);
for (i = 0; i < 8; ++i)
myp2cstr((unsigned char*)pparm->szCtl[i]);
}
// Case #1: Mac is reading Windows (Win16/32/64) plugin
if (fromwin && tomac) {
size_t i;
// Convert copyright CRLF to CR (actually, just removing LF)
char copyrightCRLF[256] = { 0 };
char* pCopyright = ©rightCRLF[0];
for (i = 0; i < strlen(pparm->szCopyright); i++) {
if (pparm->szCopyright[i] != LF) {
*pCopyright++ = pparm->szCopyright[i];
}
}
*pCopyright++ = '\0';
strcpy(pparm->szCopyright, copyrightCRLF);
// these are the only numeric fields we *have* to swap
// all the rest are bool_t flags which (if we're careful) will work in either ordering
for (i = 0; i < 8; ++i)
pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
}
// Case #2: Mac is reading Mac (in case the normal resource extraction didn't work)
// Nothing to do
// 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)
// Nothing to do
// Case #4: Windows is reading an old FilterFactory Mac file
// Note: You must read the ".rsrc" resource fork, not the standalone binary!
if (frommac && towin) {
size_t i;
// Convert CR in the copyright field to CRLF.
char copyrightCRLF[256] = { 0 };
char* pCopyright = ©rightCRLF[0];
for (i = 0; i < strlen(pparm->szCopyright); i++) {
*pCopyright++ = pparm->szCopyright[i];
if (pparm->szCopyright[i] == CR) {
*pCopyright++ = LF;
}
}
*pCopyright++ = '\0';
strcpy(pparm->szCopyright, copyrightCRLF);
// these are the only numeric fields we *have* to swap
// all the rest are bool_t flags which (if we're careful) will work in either ordering
for (i = 0; i < 8; ++i)
pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
}
return FF_LOADING_RESULT(LOADING_OK);
}
Handle readfileintohandle(FILEREF r){
FILEPOS n;
Handle h;
Ptr p;
if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){
p = PILOCKHANDLE(h,false);
if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){
PIUNLOCKHANDLE(h);
return h;
}
PIDISPOSEHANDLE(h);
}
return NULL;
}
Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) {
size_t i;
for (i = 0; i < strlen(line); i++) {
if (line[i] == '?') break; // avoid that "a?b:c" is detected as key
if (line[i] == ':') {
// Note: We are ignoring whitespaces, i.e. " A :" != "A:" (TODO: should we change this?)
if ((searchkey == NULL) || ((i == strlen(searchkey)) && (memcmp(line, searchkey, i) == 0))) {
i++; // jump over ':' char
//while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left
*content = line + i;
return true;
}
}
}
*content = line;
return false;
}
void _ffdcomp_removebrackets(char* x, char* maxptr) {
char* closingBracketPos = NULL;
Boolean openingBracketFound = false;
if (x[0] == '[') {
openingBracketFound = true;
}
x[0] = ':';
x++;
while (x < maxptr) {
if ((!openingBracketFound) && (x[0] == '[')) {
openingBracketFound = true;
x[0] = ' ';
} else if (openingBracketFound) {
if (x[0] == ']') {
closingBracketPos = x;
} else if ((x[0] == CR) || (x[0] == LF)) {
if (closingBracketPos) closingBracketPos[0] = ' '; // last closing pos before CR/LF
break;
}
}
x++;
}
}
/**
isFormula = false = > outputFile is C string.TXT linebreaks become spaces.
isFormula=true => outputFile is C string. TXT line breaks become CRLF line breaks.
*/
Boolean _picoReadProperty(char* inputFile, size_t maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) {
size_t i;
char* outputwork;
char* sline;
char* svalue;
char* inputwork;
char* inputworkinitial;
outputwork = outputFile;
sline = NULL;
svalue = NULL;
// Check parameters
if (maxOutput == 0) return false;
if (inputFile == NULL) return false;
// Let input memory be read-only, +1 for terminal zero
//char* inputwork = inputFile;
inputwork = (char*)malloc((size_t)maxInput + 1/*NUL byte*/);
inputworkinitial = inputwork;
if (inputwork == NULL) return false;
memcpy(inputwork, inputFile, maxInput);
inputwork[maxInput] = '\0'; // otherwise strstr() will crash
// Transform "FFDecomp" TXT file into the similar "PluginCommander" TXT file
if (strstr(inputwork, "Filter Factory Plugin Information:")) {
char* x;
char* k1;
char* k2;
// Metadata:
x = strstr(inputwork, "CATEGORY:");
if (x) memcpy(x, "Category:", strlen("Category:"));
x = strstr(inputwork, "TITLE:");
if (x) memcpy(x, "Title:", strlen("Title:"));
x = strstr(inputwork, "COPYRIGHT:");
if (x) memcpy(x, "Copyright:", strlen("Copyright:"));
x = strstr(inputwork, "AUTHOR:");
if (x) memcpy(x, "Author:", strlen("Author:"));
// Controls:
for (i = 0; i < 8; i++) {
k1 = (char*)malloc(strlen("Control X:") + 1/*NUL byte*/);
sprintf(k1, "Control %d:", (int)i);
x = strstr(inputwork, k1);
if (x) {
k2 = (char*)malloc(strlen("ctl[X]: ") + 1/*NUL byte*/);
sprintf(k2, "ctl[%d]: ", (int)i);
memcpy(x, k2, strlen(k2));
x += strlen("ctl[X]");
_ffdcomp_removebrackets(x, inputwork + maxInput - 1);
free(k2);
}
free(k1);
}
// Maps:
for (i = 0; i < 4; i++) {
k1 = (char*)malloc(strlen("Map X:") + 1/*NUL byte*/);
sprintf(k1, "Map %d:", (int)i);
x = strstr(inputwork, k1);
if (x) {
k2 = (char*)malloc(strlen("map[X]:") + 1/*NUL byte*/);
sprintf(k2, "map[%d]:", (int)i);
memcpy(x, k2, strlen(k2));
x += strlen("map[X]");
_ffdcomp_removebrackets(x, inputwork + maxInput - 1);
free(k2);
}
free(k1);
}
// Convert all '\r' to '\n' for the next step to be easier
for (i = 0; i < maxInput; i++) {
if (inputworkinitial[i] == CR) inputworkinitial[i] = LF;
}
x = strstr(inputwork, "\nR=\n");
if (x) memcpy(x, "\nR:\n", strlen("\nR:\n"));
x = strstr(inputwork, "\nG=\n");
if (x) memcpy(x, "\nG:\n", strlen("\nG:\n"));
x = strstr(inputwork, "\nB=\n");
if (x) memcpy(x, "\nB:\n", strlen("\nB:\n"));
x = strstr(inputwork, "\nA=\n");
if (x) memcpy(x, "\nA:\n", strlen("\nA:\n"));
}
// Replace all \r and \n with \0, so that we can parse easier
for (i = 0; i < maxInput; i++) {
if (inputworkinitial[i] == CR) inputworkinitial[i] = '\0';
else if (inputworkinitial[i] == LF) inputworkinitial[i] = '\0';
}
// Find line that contains out key
inputwork = inputworkinitial;
do {
if (inputwork > inputworkinitial + maxInput) {
// Key not found. Set output to empty string
outputwork[0] = '\0';
free(inputworkinitial);
return false;
}
sline = inputwork;
inputwork += strlen(sline) + 1;
if (inputwork - 1 > inputworkinitial + maxInput) {
// Key not found. Set output to empty string
// TODO: will that be ever called?
outputwork[0] = '\0';
free(inputworkinitial);
return false;
}
} while (!_picoLineContainsKey(sline, &svalue, property));
// Read line(s) until we find a line with another key, or the line end
do {
while ((svalue[0] == ' ') || (svalue[0] == TAB)) svalue++; // Trim left
while ((svalue[strlen(svalue) - 1] == ' ') || (svalue[strlen(svalue) - 1] == TAB)) svalue[strlen(svalue) - 1] = '\0'; // Trim right
if (strlen(svalue) > 0) {
if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) {
size_t remaining = maxOutput - (outputwork - outputFile) - 1;
//printf("BUFFER FULL (remaining = %d)\n", remaining);
memcpy(outputwork, svalue, remaining);
outputwork += remaining;
outputwork[0] = '\0';
free(inputworkinitial);
return true;
} else {
memcpy(outputwork, svalue, strlen(svalue));
outputwork += strlen(svalue);
if (isFormula) {
// Formulas: TXT line break stays line break (important if you have comments!)
outputwork[0] = CR;
outputwork[1] = LF;
outputwork += 2;
} else {
// Everything else: TXT line breaks becomes single whitespace
outputwork[0] = ' ';
outputwork++;
}
}
}
outputwork[0] = '\0';
// Process next line
if (inputwork > inputworkinitial + maxInput) break;
sline = inputwork;
inputwork += strlen(sline) + 1;
if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called?
} while (!_picoLineContainsKey(sline, &svalue, NULL));
// Remove trailing whitespace
if (outputwork > outputFile) {
outputwork -= 1;
outputwork[0] = '\0';
}
free(inputworkinitial);
return true;
}
FFLoadingResult readfile_picotxt_or_ffdecomp(StandardFileReply* sfr) {
Handle h;
FFLoadingResult res = FF_LOADING_RESULT(MSG_LOADFILE_UNKNOWN_FORMAT_ID);
FILEREF refnum;
if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
if ((h = readfileintohandle(refnum))) {
FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h);
char* q = PILOCKHANDLE(h, false);
char dummy[256];
if (_picoReadProperty(q, count, "Title", dummy, sizeof(dummy), false)) {
int i;
// Plugin infos
_picoReadProperty(q, count, "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle), false);
_picoReadProperty(q, count, "Category", gdata->parm.szCategory, sizeof(gdata->parm.szCategory), false);
_picoReadProperty(q, count, "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor), false);
_picoReadProperty(q, count, "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright), false);
//_picoReadProperty(q, count, "Filename", gdata->parm.xxx, sizeof(gdata->parm.xxx), false);
// Expressions
if (!_picoReadProperty(q, count, "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]), true))
strcpy(gdata->parm.szFormula[0], "r");
if (!_picoReadProperty(q, count, "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]), true))
strcpy(gdata->parm.szFormula[1], "g");
if (!_picoReadProperty(q, count, "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]), true))
strcpy(gdata->parm.szFormula[2], "b");
if (!_picoReadProperty(q, count, "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]), true))
strcpy(gdata->parm.szFormula[3], "a");
for (i = 0; i < 8; i++) {
if (gdata->parm.ctl_used[i]) {
int v;
char keyname[6/*strlen("ctl[X]")*/ + 1/*strlen("\0")*/], tmp[5];
// Slider names
sprintf(keyname, "ctl[%d]", i);
_picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false);
// Slider values
sprintf(keyname, "val[%d]", i);
if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
sprintf(keyname, "def[%d]", i);
if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
strcpy(tmp, "0");
}
}
v = atoi(tmp);
if (v < 0) v = 0;
else if (v > 255) v = 255;
gdata->parm.val[i] = gdata->parm.val[i] = (uint8_t)v;
}
}
// Map names
for (i = 0; i < 4; i++) {
if (gdata->parm.map_used[i]) {
char keyname[6/*strlen("map[X]")*/ + 1/*strlen("\0")*/];
sprintf(keyname, "map[%d]", i);
_picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false);
}
}
res = FF_LOADING_RESULT(LOADING_OK);
}
PIUNLOCKHANDLE(h);
PIDISPOSEHANDLE(h);
}
FSClose(refnum);
}
return res;
}
Boolean _gufReadProperty(char* fileContents, size_t argMaxInputLength, const char* section, const char* keyname, char* argOutput, size_t argMaxOutLength) {
size_t iTmp;
char* tmpFileContents, * tmpSection, * tmpStart, * tmpStart2, * tmpEnd, * tmpKeyname, * tmpStart3, * tmpStart4, * inputwork;
// Check parameters
if (argMaxOutLength == 0) return false;
if (fileContents == NULL) return false;
// Handle argMaxInputLength
//char* inputwork = fileContents;
inputwork = (char*)malloc((size_t)argMaxInputLength + 1/*NUL byte*/);
if (inputwork == NULL) return false;
memcpy(inputwork, fileContents, argMaxInputLength);
inputwork[argMaxInputLength] = '\0';
// Prepare the input file contents to make it easier parse-able
iTmp = strlen(inputwork) + strlen("\n\n[");
tmpFileContents = (char*)malloc(iTmp + 1/*NUL byte*/);
if (tmpFileContents == NULL) return false;
sprintf(tmpFileContents, "\n%s\n[", inputwork);
for (iTmp = 0; iTmp < strlen(tmpFileContents); iTmp++) {
if (tmpFileContents[iTmp] == CR) tmpFileContents[iTmp] = LF;
}
// Find the section begin
iTmp = strlen(section) + strlen("\n[]\n");
tmpSection = (char*)malloc(iTmp + 1/*NUL byte*/);
if (tmpSection == NULL) return false;
sprintf(tmpSection, "\n[%s]\n", section);
tmpStart = strstr(tmpFileContents, tmpSection);
if (tmpStart == NULL) return false;
tmpStart += iTmp;
// Find the end of the section and set a NULL terminator to it
iTmp = strlen(tmpStart) + strlen("\n");
tmpStart2 = (char*)malloc(iTmp + 1/*NUL byte*/);
if (tmpStart2 == NULL) return false;
sprintf(tmpStart2, "\n%s", tmpStart);
tmpEnd = strstr(tmpStart2, "\n[");
if (tmpEnd == NULL) return false;
tmpEnd[0] = '\0';
// Find the start of the value
iTmp = strlen(keyname) + strlen("\n=");
tmpKeyname = (char*)malloc(iTmp + 1/*NUL byte*/);
if (tmpKeyname == NULL) return false;
sprintf(tmpKeyname, "\n%s=", keyname);
tmpStart3 = strstr(tmpStart2, tmpKeyname);
if (tmpStart3 == NULL) return false;
tmpStart3 += strlen("\n");
tmpStart4 = strstr(tmpStart3, "=");
if (tmpStart4 == NULL) return false;
tmpStart4 += strlen("=");
// Find the end of the value
tmpEnd = strstr(tmpStart4, "\n");
if (tmpEnd == NULL) return false;
tmpEnd[0] = '\0';
// Copy to output
if (strlen(tmpStart4) < argMaxOutLength + 1) argMaxOutLength = strlen(tmpStart4) + 1;
memcpy(argOutput, tmpStart4, argMaxOutLength);
argOutput[argMaxOutLength - 1] = '\0';
// Free all temporary stuff
//for (iTmp = 0; iTmp < strlen(tmpFileContents); iTmp++) tmpFileContents[iTmp] = '\0';
free(tmpFileContents);
//for (iTmp = 0; iTmp < strlen(tmpSection); iTmp++) tmpSection[iTmp] = '\0';
free(tmpSection);
//for (iTmp = 0; iTmp < strlen(tmpKeyname); iTmp++) tmpKeyname[iTmp] = '\0';
free(tmpKeyname);
//for (iTmp = 0; iTmp < strlen(tmpStart2); iTmp++) tmpStart2[iTmp] = '\0';
free(tmpStart2);
// Return success
return true;
}
// Funktion zur Konvertierung von UTF-8 nach Windows-1252 in-place
void _utf8_to_windows1252(unsigned char* utf8, unsigned char* ansi, FILECOUNT* count) {
int togo = *count;
while (togo > 0) {
unsigned char byte = *utf8;
if (byte <= 0x7F) {
// Ein einzelnes Byte für ASCII-Zeichen
*ansi = byte;
ansi++;
utf8++; // Gehe zum nächsten Zeichen
togo -= 1;
} else if ((byte >= 0xC2 && byte <= 0xDF) && (*(utf8 + 1) >= 0x80 && *(utf8 + 1) <= 0xBF)) {
// Zwei-Byte UTF-8-Zeichen
unsigned char byte2 = *(utf8 + 1);
unsigned int codepoint = ((byte & 0x1F) << 6) | (byte2 & 0x3F);
if (codepoint <= 0xFF) {
// Wenn der Codepoint im Bereich von Windows-1252 liegt, füge ihn hinzu
*ansi = (char)codepoint;
ansi++;
utf8 += 2;
togo -= 2;
*count -= 1;
} else {
// Andernfalls ersetze es durch '?'
*ansi = '?';
ansi++;
utf8++;
togo -= 1;
}
}
else if ((byte >= 0xE0 && byte <= 0xEF) && (*(utf8 + 1) >= 0x80 && *(utf8 + 1) <= 0xBF) && (*(utf8 + 2) >= 0x80 && *(utf8 + 2) <= 0xBF)) {
// Drei-Byte UTF-8-Zeichen
unsigned char byte2 = *(utf8 + 1);
unsigned char byte3 = *(utf8 + 2);
unsigned int codepoint = ((byte & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
if (codepoint <= 0xFF) {
// Wenn der Codepoint im Bereich von Windows-1252 liegt, füge ihn hinzu
*ansi = (char)codepoint;
ansi++;
utf8 += 3;
togo -= 3;
*count -= 2;
} else {
// Andernfalls ersetze es durch '?'
*ansi = '?';
ansi++;
utf8++;
togo -= 1;
}
} else {
// Ungültige UTF-8-Zeichen oder Zeichen, die nicht konvertiert werden können, ersetzen
*ansi = '?';
ansi++;
utf8++;
togo -= 1;
}
}
}
FFLoadingResult readfile_guf(StandardFileReply* sfr) {
Handle h;
FFLoadingResult res = FF_LOADING_RESULT(MSG_LOADFILE_UNKNOWN_FORMAT_ID);
FILEREF refnum;
if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
if ((h = readfileintohandle(refnum))) {
FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h);
char* q = PILOCKHANDLE(h, false);
char protocol[256];
unsigned char* ansiFileContents = (unsigned char*)malloc(count);
_utf8_to_windows1252((unsigned char*)q, ansiFileContents, &count);
memcpy(q, ansiFileContents, count);
free(ansiFileContents);
if (!_gufReadProperty(q, count, "GUF", "Protocol", protocol, sizeof(protocol))) {
res = FF_LOADING_RESULT(MSG_INVALID_FILE_SIGNATURE_ID);
} else if (strcmp(protocol, "1") != 0) {
res = FF_LOADING_RESULT(MSG_INCOMPATIBLE_GUF_FILE_ID);
} else {
int i;
char tmp[256];
char* tmp2;
// Plugin infos
_gufReadProperty(q, count, "Info", "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle));
_gufReadProperty(q, count, "Info", "Category", tmp, sizeof(tmp));
tmp2 = strrchr(tmp, '/');
if (tmp2 == NULL) {
strcpy(gdata->parm.szCategory, tmp);
} else {
strcpy(gdata->parm.szCategory, tmp2+1);
}
_gufReadProperty(q, count, "Info", "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor));
_gufReadProperty(q, count, "Info", "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright));
//_gufReadProperty(q, count, "Filter Factory", "8bf", gdata->parm.xxx, sizeof(gdata->parm.xxx));
// Expressions
if (!_gufReadProperty(q, count, "Code", "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0])))
strcpy(gdata->parm.szFormula[0], "r");
if (!_gufReadProperty(q, count, "Code", "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1])))
strcpy(gdata->parm.szFormula[1], "g");
if (!_gufReadProperty(q, count, "Code", "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2])))
strcpy(gdata->parm.szFormula[2], "b");
if (!_gufReadProperty(q, count, "Code", "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3])))
strcpy(gdata->parm.szFormula[3], "a");
for (i = 0; i < 8; i++) {
if (gdata->parm.ctl_used[i]) {
int v;
char keyname[9/*strlen("Control X")*/ + 1/*strlen("\0")*/], tmp[5];
sprintf(keyname, "Control %d", i);
// Slider names
_gufReadProperty(q, count, keyname, "Label", gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]));
// Slider values
if (!_gufReadProperty(q, count, keyname, "Preset", tmp, sizeof(tmp))) {
strcpy(tmp, "0");
}
v = atoi(tmp);
if (v < 0) v = 0;
else if (v > 255) v = 255;
gdata->parm.val[i] = gdata->parm.val[i] = (uint8_t)v;
}
}
// Map names
for (i = 0; i < 4; i++) {
if (gdata->parm.map_used[i]) {
char keyname[5/*strlen("Map X")*/ + 1/*strlen("\0")*/];
sprintf(keyname, "Map %d", i);
_gufReadProperty(q, count, keyname, "Label", gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]));
}
}
res = FF_LOADING_RESULT(LOADING_OK);
}
PIUNLOCKHANDLE(h);
PIDISPOSEHANDLE(h);
}
FSClose(refnum);
}
return res;
}
FFLoadingResult readfile_afs_pff(StandardFileReply *sfr){