Rev 552 | Rev 554 | 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 |
||
536 | daniel-mar | 3 | Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.net |
541 | daniel-mar | 4 | Copyright (C) 2018-2023 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 | |
553 | daniel-mar | 39 | FFLoadingResult readparams_afs_pff(Handle h, Boolean premiereOrder){ |
552 | daniel-mar | 40 | FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
544 | daniel-mar | 41 | char linebuf[MAXLINE] = { 0 }; |
42 | char curexpr[MAXEXPR] = { 0 }; |
||
454 | daniel-mar | 43 | char *p, *dataend, *q; |
256 | daniel-mar | 44 | char c; |
45 | int linecnt, lineptr, exprcnt; |
||
46 | |||
552 | daniel-mar | 47 | //if (!h) return nilHandleErr; |
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 | |||
539 | daniel-mar | 65 | linebuf[lineptr] = '\0'; /* add terminating NUL to line buffer */ |
256 | daniel-mar | 66 | |
67 | /* process complete line */ |
||
68 | if(linecnt==0){ |
||
539 | daniel-mar | 69 | if(strcmp(linebuf,"%RGB-1.0") != 0){ |
553 | daniel-mar | 70 | res = MSG_INVALID_FILE_SIGNATURE_ID; |
256 | daniel-mar | 71 | break; |
72 | } |
||
73 | }else if(linecnt<=8){ |
||
453 | daniel-mar | 74 | int v; |
75 | v = atoi(linebuf); |
||
76 | if (v < 0) v = 0; |
||
456 | daniel-mar | 77 | else if (v > 255) v = 255; |
550 | daniel-mar | 78 | gdata->parm.val[linecnt-1] = (uint8_t)v; |
256 | daniel-mar | 79 | }else{ |
80 | if(lineptr){ |
||
81 | /* it's not an empty line; append it to current expr string */ |
||
544 | daniel-mar | 82 | if( (q-curexpr) + lineptr >= MAXEXPR) { |
552 | daniel-mar | 83 | res = MSG_EXPRESSION1024_FOUND_ID; |
256 | daniel-mar | 84 | break; |
85 | } |
||
86 | q = cat(q,linebuf); |
||
87 | }else{ |
||
88 | /* it's an empty line: we've completed the expr string */ |
||
539 | daniel-mar | 89 | *q = '\0'; |
553 | daniel-mar | 90 | if (premiereOrder) { |
91 | // Premiere has the order BGRA , while Photoshop (and our internal order) is RGBA |
||
92 | if (exprcnt == 0) strcpy(gdata->parm.szFormula[2], curexpr); |
||
93 | else if (exprcnt == 2) strcpy(gdata->parm.szFormula[0], curexpr); |
||
94 | else strcpy(gdata->parm.szFormula[exprcnt], curexpr); |
||
95 | } |
||
96 | else { |
||
97 | strcpy(gdata->parm.szFormula[exprcnt], curexpr); |
||
98 | } |
||
256 | daniel-mar | 99 | |
100 | if(++exprcnt == 4){ |
||
552 | daniel-mar | 101 | res = 0; |
256 | daniel-mar | 102 | break; /* got everything we want */ |
103 | } |
||
104 | |||
105 | q = curexpr; /* empty current expr, ready for next one */ |
||
106 | } |
||
107 | } |
||
108 | |||
109 | ++linecnt; |
||
110 | lineptr = 0; |
||
111 | }else{ |
||
112 | /* store character */ |
||
113 | if(c=='\\'){ /* escape sequence */ |
||
114 | if(p < dataend){ |
||
115 | c = *p++; |
||
116 | switch(c){ |
||
117 | case 'r': |
||
268 | daniel-mar | 118 | #if WIN_ENV |
119 | c = CR; |
||
544 | daniel-mar | 120 | if (lineptr < MAXLINE-1) |
274 | daniel-mar | 121 | linebuf[lineptr++] = c; |
268 | daniel-mar | 122 | c = LF; |
123 | #else |
||
124 | c = CR; |
||
125 | #endif |
||
126 | break; |
||
256 | daniel-mar | 127 | case '\\': break; |
408 | daniel-mar | 128 | //default: |
492 | daniel-mar | 129 | // if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("Unknown escape sequence in input.")); // TODO (Not so important): TRANSLATE |
256 | daniel-mar | 130 | } |
492 | daniel-mar | 131 | }//else if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("truncated escape sequence ends input")); // TODO (Not so important): TRANSLATE |
256 | daniel-mar | 132 | } |
133 | |||
544 | daniel-mar | 134 | if(lineptr < MAXLINE-1) |
256 | daniel-mar | 135 | linebuf[lineptr++] = c; |
136 | } |
||
137 | } |
||
138 | |||
139 | PIUNLOCKHANDLE(h); |
||
140 | |||
141 | return res; |
||
142 | } |
||
143 | |||
144 | void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) { |
||
145 | int i; |
||
146 | |||
147 | photoshop->cbSize = sizeof(PARM_T); |
||
148 | photoshop->standalone = premiere->standalone; |
||
149 | for (i=0;i<8;++i) |
||
272 | daniel-mar | 150 | photoshop->val[i] = premiere->val[i]; |
256 | daniel-mar | 151 | photoshop->popDialog = premiere->popDialog; |
152 | photoshop->unknown1 = premiere->unknown1; |
||
153 | photoshop->unknown2 = premiere->unknown2; |
||
154 | photoshop->unknown3 = premiere->unknown3; |
||
155 | for (i=0;i<4;++i) |
||
272 | daniel-mar | 156 | photoshop->map_used[i] = premiere->map_used[i]; |
256 | daniel-mar | 157 | for (i=0;i<8;++i) |
272 | daniel-mar | 158 | photoshop->ctl_used[i] = premiere->ctl_used[i]; |
393 | daniel-mar | 159 | sprintf(photoshop->szCategory, "Filter Factory"); // Premiere plugins do not have a category attribute |
256 | daniel-mar | 160 | photoshop->iProtected = 0; // Premiere plugins do not have a protect flag |
393 | daniel-mar | 161 | memcpy((void*)photoshop->szTitle, (void*)premiere->szTitle, sizeof(photoshop->szTitle)); |
162 | memcpy((void*)photoshop->szCopyright, (void*)premiere->szCopyright, sizeof(photoshop->szCopyright)); |
||
163 | memcpy((void*)photoshop->szAuthor, (void*)premiere->szAuthor, sizeof(photoshop->szAuthor)); |
||
256 | daniel-mar | 164 | for (i=0;i<4;++i) |
393 | daniel-mar | 165 | memcpy((void*)photoshop->szMap[i], (void*)premiere->szMap[i], sizeof(photoshop->szMap[i])); |
256 | daniel-mar | 166 | for (i=0;i<8;++i) |
393 | daniel-mar | 167 | memcpy((void*)photoshop->szCtl[i], (void*)premiere->szCtl[i], sizeof(photoshop->szCtl[i])); |
256 | daniel-mar | 168 | |
169 | if (premiere->singleExpression) { |
||
393 | daniel-mar | 170 | memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3])); |
171 | memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3])); |
||
172 | memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3])); |
||
173 | memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3])); |
||
256 | daniel-mar | 174 | } else { |
393 | daniel-mar | 175 | memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[2], sizeof(photoshop->szFormula[2])); |
176 | memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[1], sizeof(photoshop->szFormula[1])); |
||
177 | memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[0], sizeof(photoshop->szFormula[0])); |
||
178 | memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3])); |
||
256 | daniel-mar | 179 | } |
180 | } |
||
181 | |||
366 | daniel-mar | 182 | char* _ffx_read_str(char** q) { |
183 | uint32_t len; |
||
184 | char* val; |
||
185 | |||
186 | len = *((uint32_t*)*q); |
||
187 | *q += sizeof(uint32_t); |
||
454 | daniel-mar | 188 | val = (char*)malloc((size_t)len + 1); |
366 | daniel-mar | 189 | if (val != NULL) { |
190 | memcpy(val, (char*)*q, len); |
||
539 | daniel-mar | 191 | val[len] = '\0'; |
366 | daniel-mar | 192 | } |
193 | *q += len; |
||
194 | return val; |
||
195 | } |
||
196 | |||
552 | daniel-mar | 197 | FFLoadingResult readfile_ffx(StandardFileReply* sfr) { |
366 | daniel-mar | 198 | Handle h; |
552 | daniel-mar | 199 | FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
366 | daniel-mar | 200 | FILEREF refnum; |
201 | uint32_t len; |
||
202 | char* val; |
||
367 | daniel-mar | 203 | int format_version = -1; |
366 | daniel-mar | 204 | int i; |
205 | |||
206 | if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) { |
||
207 | if ((h = readfileintohandle(refnum))) { |
||
208 | char* q = (char*)PILOCKHANDLE(h, false); |
||
209 | |||
210 | len = *((uint32_t*)q); |
||
211 | if (len == 6) { |
||
212 | val = _ffx_read_str(&q); |
||
367 | daniel-mar | 213 | if (strcmp(val, "FFX1.0") == 0) format_version = 10; |
214 | else if (strcmp(val, "FFX1.1") == 0) format_version = 11; |
||
215 | else if (strcmp(val, "FFX1.2") == 0) format_version = 12; |
||
366 | daniel-mar | 216 | free(val); |
367 | daniel-mar | 217 | if (format_version > 0) { |
492 | daniel-mar | 218 | simplewarning_id(MSG_FILTERS_UNLIMITED_WARNING_ID); |
368 | daniel-mar | 219 | |
366 | daniel-mar | 220 | val = _ffx_read_str(&q); |
541 | daniel-mar | 221 | if (strlen(val) >= sizeof(gdata->parm.szTitle)) { |
222 | val[sizeof(gdata->parm.szTitle) - 1] = '\0'; |
||
223 | } |
||
393 | daniel-mar | 224 | strcpy(gdata->parm.szTitle, val); |
366 | daniel-mar | 225 | free(val); |
226 | |||
227 | val = _ffx_read_str(&q); |
||
541 | daniel-mar | 228 | if (strlen(val) >= sizeof(gdata->parm.szCategory)) { |
229 | val[sizeof(gdata->parm.szCategory) - 1] = '\0'; |
||
230 | } |
||
393 | daniel-mar | 231 | strcpy(gdata->parm.szCategory, val); |
366 | daniel-mar | 232 | free(val); |
233 | |||
234 | val = _ffx_read_str(&q); |
||
541 | daniel-mar | 235 | if (strlen(val) >= sizeof(gdata->parm.szAuthor)) { |
236 | val[sizeof(gdata->parm.szAuthor) - 1] = '\0'; |
||
237 | } |
||
393 | daniel-mar | 238 | strcpy(gdata->parm.szAuthor, val); |
366 | daniel-mar | 239 | free(val); |
240 | |||
241 | val = _ffx_read_str(&q); |
||
541 | daniel-mar | 242 | if (strlen(val) >= sizeof(gdata->parm.szCopyright)) { |
243 | val[sizeof(gdata->parm.szCopyright) - 1] = '\0'; |
||
244 | } |
||
393 | daniel-mar | 245 | strcpy(gdata->parm.szCopyright, val); |
366 | daniel-mar | 246 | free(val); |
247 | |||
248 | // Channels I, R, G, B, A |
||
249 | for (i = 0; i < 4; i++) { |
||
250 | val = _ffx_read_str(&q); |
||
251 | if (i == 0) { |
||
370 | daniel-mar | 252 | char* val2 = _ffx_read_str(&q); |
366 | daniel-mar | 253 | if (strcmp(val, "0") != 0) { |
254 | // "Intro channel" existing |
||
370 | daniel-mar | 255 | // C++ wrong warning: Using uninitialized memory "val2" (C6001) |
256 | #pragma warning(suppress : 6001) |
||
541 | daniel-mar | 257 | char* combined = (char*)malloc(strlen(val) + strlen(",") + strlen(val2) + 1/*NUL byte*/); |
370 | daniel-mar | 258 | if (combined != NULL) { |
259 | sprintf(combined, "%s,%s", val, val2); |
||
260 | free(val); |
||
261 | free(val2); |
||
262 | val = combined; |
||
263 | } |
||
366 | daniel-mar | 264 | } |
265 | else { |
||
266 | free(val); |
||
370 | daniel-mar | 267 | val = val2; |
366 | daniel-mar | 268 | } |
269 | } |
||
393 | daniel-mar | 270 | if (strlen(val) >= sizeof(gdata->parm.szFormula[i])) { |
368 | daniel-mar | 271 | if (i == 0) { |
492 | daniel-mar | 272 | simplealert_id(MSG_FORMULA_IR_1023_TRUNCATED_ID); |
368 | daniel-mar | 273 | } |
274 | else if (i == 1) { |
||
492 | daniel-mar | 275 | simplealert_id(MSG_FORMULA_G_1023_TRUNCATED_ID); |
368 | daniel-mar | 276 | } |
277 | else if (i == 2) { |
||
492 | daniel-mar | 278 | simplealert_id(MSG_FORMULA_B_1023_TRUNCATED_ID); |
368 | daniel-mar | 279 | } |
280 | else if (i == 3) { |
||
492 | daniel-mar | 281 | simplealert_id(MSG_FORMULA_A_1023_TRUNCATED_ID); |
368 | daniel-mar | 282 | } |
370 | daniel-mar | 283 | // C++ wrong warning: Buffer overflow (C6386) |
284 | #pragma warning(suppress : 6386) |
||
393 | daniel-mar | 285 | val[sizeof(gdata->parm.szFormula[i]) - 1] = '\0'; |
368 | daniel-mar | 286 | } |
393 | daniel-mar | 287 | strcpy(gdata->parm.szFormula[i], val); |
366 | daniel-mar | 288 | free(val); |
289 | } |
||
290 | |||
291 | // Sliders |
||
292 | for (i = 0; i < 8; i++) { |
||
367 | daniel-mar | 293 | char* sliderName; |
453 | daniel-mar | 294 | int v; |
366 | daniel-mar | 295 | val = _ffx_read_str(&q); |
367 | daniel-mar | 296 | sliderName = val; |
297 | if (format_version >= 12) { |
||
298 | // Format FFX1.2 has prefixes {S} = Slider, {C} = Checkbox, none = Slider |
||
299 | if ((sliderName[0] == '{') && (sliderName[1] == 'S') && (sliderName[2] == '}')) sliderName += 3; |
||
300 | else if ((sliderName[0] == '{') && (sliderName[1] == 'C') && (sliderName[2] == '}')) sliderName += 3; |
||
301 | } |
||
541 | daniel-mar | 302 | if (strlen(val) >= sizeof(gdata->parm.szCtl[i])) { |
303 | val[sizeof(gdata->parm.szCtl[i]) - 1] = '\0'; |
||
304 | } |
||
393 | daniel-mar | 305 | strcpy(gdata->parm.szCtl[i], sliderName); |
366 | daniel-mar | 306 | free(val); |
367 | daniel-mar | 307 | gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q); |
366 | daniel-mar | 308 | q += sizeof(byte); |
309 | gdata->parm.val[i] = *((uint32_t*)q); |
||
453 | daniel-mar | 310 | v = *((uint32_t*)q); |
311 | if (v < 0) v = 0; |
||
456 | daniel-mar | 312 | else if (v > 255) v = 255; |
550 | daniel-mar | 313 | gdata->parm.val[i] = (uint8_t)v; |
366 | daniel-mar | 314 | q += sizeof(uint32_t); |
315 | } |
||
316 | |||
390 | daniel-mar | 317 | // Maps (are not part of the format!) |
393 | daniel-mar | 318 | strcpy(gdata->parm.szMap[0], "Map 0:"); |
319 | strcpy(gdata->parm.szMap[1], "Map 1:"); |
||
320 | strcpy(gdata->parm.szMap[2], "Map 2:"); |
||
321 | strcpy(gdata->parm.szMap[3], "Map 3:"); |
||
390 | daniel-mar | 322 | |
552 | daniel-mar | 323 | res = 0; |
366 | daniel-mar | 324 | } |
325 | } |
||
326 | PIDISPOSEHANDLE(h); |
||
327 | } |
||
328 | FSClose(refnum); |
||
329 | } |
||
330 | |||
331 | if (res) gdata->obfusc = false; |
||
332 | return res; |
||
333 | } |
||
334 | |||
552 | daniel-mar | 335 | FFLoadingResult readfile_8bf(StandardFileReply *sfr){ |
256 | daniel-mar | 336 | unsigned char magic[2]; |
337 | FILECOUNT count; |
||
338 | Handle h; |
||
552 | daniel-mar | 339 | FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
256 | daniel-mar | 340 | FILEREF refnum; |
341 | |||
342 | if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){ |
||
343 | // check DOS EXE magic number |
||
344 | count = 2; |
||
345 | if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){ |
||
309 | daniel-mar | 346 | 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 | 347 | if( (h = readfileintohandle(refnum)) ){ |
348 | long *q = (long*)PILOCKHANDLE(h,false); |
||
349 | |||
350 | // look for signature at start of valid PARM resource |
||
351 | // This signature is observed in Filter Factory standalones. |
||
352 | for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q ) |
||
353 | { |
||
394 | daniel-mar | 354 | res = readPARM(&gdata->parm, (Ptr)q); |
256 | daniel-mar | 355 | if (res) break; |
356 | } |
||
341 | daniel-mar | 357 | |
256 | daniel-mar | 358 | PIDISPOSEHANDLE(h); |
359 | } |
||
360 | } |
||
361 | } // else no point in proceeding |
||
362 | FSClose(refnum); |
||
363 | }else |
||
552 | daniel-mar | 364 | res = MSG_CANNOT_OPEN_FILE_ID; |
256 | daniel-mar | 365 | |
366 | if (res) gdata->obfusc = false; |
||
367 | return res; |
||
368 | } |
||
369 | |||
552 | daniel-mar | 370 | FFLoadingResult readPARM(PARM_T* pparm, Ptr p){ |
394 | daniel-mar | 371 | Boolean towin, tomac, fromwin, frommac; |
372 | unsigned int signature = *((unsigned int*)p); |
||
373 | unsigned int standalone = *((unsigned int*)p+1); |
||
256 | daniel-mar | 374 | |
394 | daniel-mar | 375 | // Find out our OS ("reader") the OS of the plugin ("source") |
376 | #ifdef MAC_ENV |
||
377 | towin = false; |
||
378 | tomac = true; |
||
379 | fromwin = ((EndianS32_LtoN(signature) == PARM_SIZE) || |
||
380 | (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) || |
||
381 | (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1; |
||
382 | frommac = ((signature == PARM_SIZE) || |
||
383 | (signature == PARM_SIZE_PREMIERE) || |
||
384 | (signature == PARM_SIG_MAC)) && standalone == 1; |
||
385 | #else |
||
386 | towin = true; |
||
387 | tomac = false; |
||
388 | fromwin = ((signature == PARM_SIZE) || |
||
389 | (signature == PARM_SIZE_PREMIERE) || |
||
390 | (signature == PARM_SIG_MAC)) && standalone == 1; |
||
391 | frommac = ((EndianS32_LtoN(signature) == PARM_SIZE) || |
||
392 | (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) || |
||
393 | (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1; |
||
394 | #endif |
||
395 | |||
396 | // Is it a valid signature? |
||
397 | if (!fromwin && !frommac) { |
||
398 | // No valid signature found |
||
552 | daniel-mar | 399 | return MSG_INVALID_FILE_SIGNATURE_ID; |
394 | daniel-mar | 400 | } |
401 | |||
402 | // Does it come from Premiere or Photoshop? |
||
403 | // Initialize pparm |
||
404 | if ((signature == PARM_SIZE_PREMIERE) || (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE)) { |
||
405 | // It comes from Premiere. Swap R and B channel and convert to a Photoshop PARM_T |
||
256 | daniel-mar | 406 | convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p); |
407 | } else { |
||
394 | daniel-mar | 408 | // It is already Photoshop. Just copy to pparm. |
409 | memcpy(pparm, p, sizeof(PARM_T)); |
||
256 | daniel-mar | 410 | } |
411 | |||
394 | daniel-mar | 412 | // Do we need to do string conversion? |
413 | if (frommac) { |
||
456 | daniel-mar | 414 | int i; |
415 | |||
394 | daniel-mar | 416 | /* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */ |
393 | daniel-mar | 417 | myp2cstr((unsigned char*)pparm->szCategory); |
418 | myp2cstr((unsigned char*)pparm->szTitle); |
||
419 | myp2cstr((unsigned char*)pparm->szCopyright); |
||
420 | myp2cstr((unsigned char*)pparm->szAuthor); |
||
394 | daniel-mar | 421 | for (i = 0; i < 4; ++i) |
393 | daniel-mar | 422 | myp2cstr((unsigned char*)pparm->szMap[i]); |
394 | daniel-mar | 423 | for (i = 0; i < 8; ++i) |
393 | daniel-mar | 424 | myp2cstr((unsigned char*)pparm->szCtl[i]); |
256 | daniel-mar | 425 | } |
426 | |||
394 | daniel-mar | 427 | // Case #1: Mac is reading Windows (Win16/32/64) plugin |
428 | if (fromwin && tomac) { |
||
429 | size_t i; |
||
430 | |||
431 | // Convert copyright CRLF to CR (actually, just removing LF) |
||
454 | daniel-mar | 432 | char copyrightCRLF[256] = { 0 }; |
456 | daniel-mar | 433 | char* pCopyright = ©rightCRLF[0]; |
394 | daniel-mar | 434 | for (i = 0; i < strlen(pparm->szCopyright); i++) { |
435 | if (pparm->szCopyright[i] != LF) { |
||
456 | daniel-mar | 436 | *pCopyright++ = pparm->szCopyright[i]; |
394 | daniel-mar | 437 | } |
438 | } |
||
456 | daniel-mar | 439 | *pCopyright++ = '\0'; |
394 | daniel-mar | 440 | strcpy(pparm->szCopyright, copyrightCRLF); |
441 | |||
442 | // these are the only numeric fields we *have* to swap |
||
443 | // all the rest are bool_t flags which (if we're careful) will work in either ordering |
||
444 | for (i = 0; i < 8; ++i) |
||
445 | pparm->val[i] = EndianS32_LtoN(pparm->val[i]); |
||
446 | } |
||
447 | |||
448 | // Case #2: Mac is reading Mac (in case the normal resource extraction didn't work) |
||
449 | // Nothing to do |
||
450 | |||
451 | // 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) |
||
452 | // Nothing to do |
||
453 | |||
454 | // Case #4: Windows is reading an old FilterFactory Mac file |
||
455 | // Note: You must read the ".rsrc" resource fork, not the standalone binary! |
||
456 | if (frommac && towin) { |
||
457 | size_t i; |
||
458 | |||
459 | // Convert CR in the copyright field to CRLF. |
||
454 | daniel-mar | 460 | char copyrightCRLF[256] = { 0 }; |
456 | daniel-mar | 461 | char* pCopyright = ©rightCRLF[0]; |
394 | daniel-mar | 462 | for (i = 0; i < strlen(pparm->szCopyright); i++) { |
456 | daniel-mar | 463 | *pCopyright++ = pparm->szCopyright[i]; |
394 | daniel-mar | 464 | if (pparm->szCopyright[i] == CR) { |
456 | daniel-mar | 465 | *pCopyright++ = LF; |
394 | daniel-mar | 466 | } |
467 | } |
||
456 | daniel-mar | 468 | *pCopyright++ = '\0'; |
394 | daniel-mar | 469 | strcpy(pparm->szCopyright, copyrightCRLF); |
470 | |||
471 | // these are the only numeric fields we *have* to swap |
||
472 | // all the rest are bool_t flags which (if we're careful) will work in either ordering |
||
473 | for (i = 0; i < 8; ++i) |
||
474 | pparm->val[i] = EndianS32_LtoN(pparm->val[i]); |
||
475 | } |
||
476 | |||
552 | daniel-mar | 477 | return 0; |
256 | daniel-mar | 478 | } |
479 | |||
480 | Handle readfileintohandle(FILEREF r){ |
||
481 | FILEPOS n; |
||
482 | Handle h; |
||
483 | Ptr p; |
||
484 | |||
485 | if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){ |
||
486 | p = PILOCKHANDLE(h,false); |
||
487 | if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){ |
||
488 | PIUNLOCKHANDLE(h); |
||
489 | return h; |
||
490 | } |
||
491 | PIDISPOSEHANDLE(h); |
||
492 | } |
||
493 | return NULL; |
||
494 | } |
||
495 | |||
385 | daniel-mar | 496 | Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) { |
386 | daniel-mar | 497 | size_t i; |
498 | for (i = 0; i < strlen(line); i++) { |
||
388 | daniel-mar | 499 | if (line[i] == '?') break; // avoid that "a?b:c" is detected as key |
385 | daniel-mar | 500 | if (line[i] == ':') { |
386 | daniel-mar | 501 | // Note: We are ignoring whitespaces, i.e. " A :" != "A:" (TODO: should we change this?) |
502 | if ((searchkey == NULL) || ((i == strlen(searchkey)) && (memcmp(line, searchkey, i) == 0))) { |
||
503 | i++; // jump over ':' char |
||
387 | daniel-mar | 504 | //while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left |
385 | daniel-mar | 505 | *content = line + i; |
506 | return true; |
||
507 | } |
||
508 | } |
||
509 | } |
||
510 | *content = line; |
||
511 | return false; |
||
512 | } |
||
513 | |||
388 | daniel-mar | 514 | void _ffdcomp_removebrackets(char* x, char* maxptr) { |
515 | char* closingBracketPos = NULL; |
||
516 | Boolean openingBracketFound = false; |
||
517 | if (x[0] == '[') { |
||
518 | openingBracketFound = true; |
||
519 | } |
||
520 | x[0] = ':'; |
||
521 | x++; |
||
522 | while (x < maxptr) { |
||
523 | if ((!openingBracketFound) && (x[0] == '[')) { |
||
524 | openingBracketFound = true; |
||
525 | x[0] = ' '; |
||
526 | } |
||
527 | else if (openingBracketFound) { |
||
528 | if (x[0] == ']') { |
||
529 | closingBracketPos = x; |
||
530 | } |
||
531 | else if ((x[0] == CR) || (x[0] == LF)) { |
||
532 | if (closingBracketPos) closingBracketPos[0] = ' '; // last closing pos before CR/LF |
||
533 | break; |
||
534 | } |
||
535 | } |
||
536 | x++; |
||
537 | } |
||
538 | } |
||
539 | |||
393 | daniel-mar | 540 | // isFormula=false => outputFile is C string. TXT linebreaks become spaces. |
386 | daniel-mar | 541 | // isFormula=true => outputFile is C string. TXT line breaks become CRLF line breaks |
505 | daniel-mar | 542 | Boolean _picoReadProperty(char* inputFile, size_t maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) { |
543 | size_t i; |
||
386 | daniel-mar | 544 | char* outputwork; |
545 | char* sline; |
||
546 | char* svalue; |
||
547 | char* inputwork; |
||
548 | char* inputworkinitial; |
||
549 | outputwork = outputFile; |
||
550 | sline = NULL; |
||
551 | svalue = NULL; |
||
385 | daniel-mar | 552 | // Check parameters |
386 | daniel-mar | 553 | if (maxOutput == 0) return false; |
539 | daniel-mar | 554 | if (inputFile == NULL) return false; |
385 | daniel-mar | 555 | // Let input memory be read-only, +1 for terminal zero |
556 | //char* inputwork = inputFile; |
||
541 | daniel-mar | 557 | inputwork = (char*)malloc((size_t)maxInput + 1/*NUL byte*/); |
386 | daniel-mar | 558 | inputworkinitial = inputwork; |
539 | daniel-mar | 559 | if (inputwork == NULL) return false; |
385 | daniel-mar | 560 | memcpy(inputwork, inputFile, maxInput); |
539 | daniel-mar | 561 | inputwork[maxInput] = '\0'; // otherwise strstr() will crash |
388 | daniel-mar | 562 | |
563 | // Transform "FFDecomp" TXT file into the similar "PluginCommander" TXT file |
||
564 | if (strstr(inputwork, "Filter Factory Plugin Information:")) { |
||
565 | char* x; |
||
566 | char* k1; |
||
567 | char* k2; |
||
568 | // Metadata: |
||
569 | x = strstr(inputwork, "CATEGORY:"); |
||
389 | daniel-mar | 570 | if (x) memcpy(x, "Category:", strlen("Category:")); |
388 | daniel-mar | 571 | x = strstr(inputwork, "TITLE:"); |
389 | daniel-mar | 572 | if (x) memcpy(x, "Title:", strlen("Title:")); |
388 | daniel-mar | 573 | x = strstr(inputwork, "COPYRIGHT:"); |
389 | daniel-mar | 574 | if (x) memcpy(x, "Copyright:", strlen("Copyright:")); |
388 | daniel-mar | 575 | x = strstr(inputwork, "AUTHOR:"); |
389 | daniel-mar | 576 | if (x) memcpy(x, "Author:", strlen("Author:")); |
388 | daniel-mar | 577 | // Controls: |
578 | for (i = 0; i < 8; i++) { |
||
541 | daniel-mar | 579 | k1 = (char*)malloc(strlen("Control X:") + 1/*NUL byte*/); |
505 | daniel-mar | 580 | sprintf(k1, "Control %d:", (int)i); |
388 | daniel-mar | 581 | x = strstr(inputwork, k1); |
582 | if (x) { |
||
541 | daniel-mar | 583 | k2 = (char*)malloc(strlen("ctl[X]: ") + 1/*NUL byte*/); |
505 | daniel-mar | 584 | sprintf(k2, "ctl[%d]: ", (int)i); |
389 | daniel-mar | 585 | memcpy(x, k2, strlen(k2)); |
388 | daniel-mar | 586 | x += strlen("ctl[X]"); |
587 | _ffdcomp_removebrackets(x, inputwork + maxInput - 1); |
||
588 | free(k2); |
||
589 | } |
||
590 | free(k1); |
||
591 | } |
||
592 | // Maps: |
||
593 | for (i = 0; i < 4; i++) { |
||
541 | daniel-mar | 594 | k1 = (char*)malloc(strlen("Map X:") + 1/*NUL byte*/); |
505 | daniel-mar | 595 | sprintf(k1, "Map %d:", (int)i); |
388 | daniel-mar | 596 | x = strstr(inputwork, k1); |
597 | if (x) { |
||
541 | daniel-mar | 598 | k2 = (char*)malloc(strlen("map[X]:") + 1/*NUL byte*/); |
505 | daniel-mar | 599 | sprintf(k2, "map[%d]:", (int)i); |
389 | daniel-mar | 600 | memcpy(x, k2, strlen(k2)); |
388 | daniel-mar | 601 | x += strlen("map[X]"); |
602 | _ffdcomp_removebrackets(x, inputwork + maxInput - 1); |
||
603 | free(k2); |
||
604 | } |
||
605 | free(k1); |
||
606 | } |
||
607 | // Convert all '\r' to '\n' for the next step to be easier |
||
608 | for (i = 0; i < maxInput; i++) { |
||
609 | if (inputworkinitial[i] == CR) inputworkinitial[i] = LF; |
||
610 | } |
||
611 | x = strstr(inputwork, "\nR=\n"); |
||
389 | daniel-mar | 612 | if (x) memcpy(x, "\nR:\n", strlen("\nR:\n")); |
388 | daniel-mar | 613 | x = strstr(inputwork, "\nG=\n"); |
389 | daniel-mar | 614 | if (x) memcpy(x, "\nG:\n", strlen("\nG:\n")); |
388 | daniel-mar | 615 | x = strstr(inputwork, "\nB=\n"); |
389 | daniel-mar | 616 | if (x) memcpy(x, "\nB:\n", strlen("\nB:\n")); |
388 | daniel-mar | 617 | x = strstr(inputwork, "\nA=\n"); |
389 | daniel-mar | 618 | if (x) memcpy(x, "\nA:\n", strlen("\nA:\n")); |
388 | daniel-mar | 619 | } |
385 | daniel-mar | 620 | // Replace all \r and \n with \0, so that we can parse easier |
386 | daniel-mar | 621 | for (i = 0; i < maxInput; i++) { |
539 | daniel-mar | 622 | if (inputworkinitial[i] == CR) inputworkinitial[i] = '\0'; |
623 | else if (inputworkinitial[i] == LF) inputworkinitial[i] = '\0'; |
||
385 | daniel-mar | 624 | } |
388 | daniel-mar | 625 | |
385 | daniel-mar | 626 | // Find line that contains out key |
388 | daniel-mar | 627 | inputwork = inputworkinitial; |
385 | daniel-mar | 628 | do { |
629 | if (inputwork > inputworkinitial + maxInput) { |
||
630 | // Key not found. Set output to empty string |
||
539 | daniel-mar | 631 | outputwork[0] = '\0'; |
385 | daniel-mar | 632 | free(inputworkinitial); |
633 | return false; |
||
634 | } |
||
635 | sline = inputwork; |
||
636 | inputwork += strlen(sline) + 1; |
||
637 | if (inputwork - 1 > inputworkinitial + maxInput) { |
||
638 | // Key not found. Set output to empty string |
||
639 | // TODO: will that be ever called? |
||
539 | daniel-mar | 640 | outputwork[0] = '\0'; |
385 | daniel-mar | 641 | free(inputworkinitial); |
642 | return false; |
||
643 | } |
||
644 | } while (!_picoLineContainsKey(sline, &svalue, property)); |
||
388 | daniel-mar | 645 | |
385 | daniel-mar | 646 | // Read line(s) until we find a line with another key, or the line end |
647 | do { |
||
387 | daniel-mar | 648 | while ((svalue[0] == ' ') || (svalue[0] == TAB)) svalue++; // Trim left |
539 | daniel-mar | 649 | while ((svalue[strlen(svalue) - 1] == ' ') || (svalue[strlen(svalue) - 1] == TAB)) svalue[strlen(svalue) - 1] = '\0'; // Trim right |
386 | daniel-mar | 650 | |
385 | daniel-mar | 651 | if (strlen(svalue) > 0) { |
393 | daniel-mar | 652 | if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) { |
444 | daniel-mar | 653 | size_t remaining = maxOutput - (outputwork - outputFile) - 1; |
385 | daniel-mar | 654 | //printf("BUFFER FULL (remaining = %d)\n", remaining); |
655 | memcpy(outputwork, svalue, remaining); |
||
656 | outputwork += remaining; |
||
539 | daniel-mar | 657 | outputwork[0] = '\0'; |
385 | daniel-mar | 658 | free(inputworkinitial); |
659 | return true; |
||
660 | } |
||
661 | else { |
||
662 | memcpy(outputwork, svalue, strlen(svalue)); |
||
663 | outputwork += strlen(svalue); |
||
386 | daniel-mar | 664 | if (isFormula) { |
665 | // Formulas: TXT line break stays line break (important if you have comments!) |
||
388 | daniel-mar | 666 | outputwork[0] = CR; |
667 | outputwork[1] = LF; |
||
387 | daniel-mar | 668 | outputwork += 2; |
386 | daniel-mar | 669 | } |
670 | else { |
||
671 | // Everything else: TXT line breaks becomes single whitespace |
||
672 | outputwork[0] = ' '; |
||
673 | outputwork++; |
||
674 | } |
||
385 | daniel-mar | 675 | } |
676 | } |
||
539 | daniel-mar | 677 | outputwork[0] = '\0'; |
385 | daniel-mar | 678 | |
679 | // Process next line |
||
680 | if (inputwork > inputworkinitial + maxInput) break; |
||
681 | sline = inputwork; |
||
682 | inputwork += strlen(sline) + 1; |
||
386 | daniel-mar | 683 | if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called? |
385 | daniel-mar | 684 | } while (!_picoLineContainsKey(sline, &svalue, NULL)); |
388 | daniel-mar | 685 | |
385 | daniel-mar | 686 | // Remove trailing whitespace |
687 | if (outputwork > outputFile) { |
||
688 | outputwork -= 1; |
||
539 | daniel-mar | 689 | outputwork[0] = '\0'; |
385 | daniel-mar | 690 | } |
691 | free(inputworkinitial); |
||
692 | return true; |
||
693 | } |
||
694 | |||
552 | daniel-mar | 695 | FFLoadingResult readfile_picotxt_or_ffdecomp(StandardFileReply* sfr) { |
385 | daniel-mar | 696 | Handle h; |
552 | daniel-mar | 697 | FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
385 | daniel-mar | 698 | FILEREF refnum; |
699 | |||
552 | daniel-mar | 700 | if (!fileHasExtension(sfr, TEXT(".txt"))) return MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
433 | daniel-mar | 701 | |
385 | daniel-mar | 702 | if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) { |
703 | if ((h = readfileintohandle(refnum))) { |
||
505 | daniel-mar | 704 | FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h); |
385 | daniel-mar | 705 | char* q = PILOCKHANDLE(h, false); |
706 | |||
541 | daniel-mar | 707 | char dummy[256]; |
708 | if (_picoReadProperty(q, count, "Title", dummy, sizeof(dummy), false)) { |
||
386 | daniel-mar | 709 | int i; |
385 | daniel-mar | 710 | |
711 | // Plugin infos |
||
393 | daniel-mar | 712 | _picoReadProperty(q, count, "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle), false); |
713 | _picoReadProperty(q, count, "Category", gdata->parm.szCategory, sizeof(gdata->parm.szCategory), false); |
||
714 | _picoReadProperty(q, count, "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor), false); |
||
715 | _picoReadProperty(q, count, "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright), false); |
||
716 | //_picoReadProperty(q, count, "Filename", gdata->parm.xxx, sizeof(gdata->parm.xxx), false); |
||
385 | daniel-mar | 717 | |
718 | // Expressions |
||
393 | daniel-mar | 719 | if (!_picoReadProperty(q, count, "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]), true)) |
720 | strcpy(gdata->parm.szFormula[0], "r"); |
||
721 | if (!_picoReadProperty(q, count, "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]), true)) |
||
722 | strcpy(gdata->parm.szFormula[1], "g"); |
||
723 | if (!_picoReadProperty(q, count, "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]), true)) |
||
724 | strcpy(gdata->parm.szFormula[2], "b"); |
||
725 | if (!_picoReadProperty(q, count, "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]), true)) |
||
726 | strcpy(gdata->parm.szFormula[3], "a"); |
||
385 | daniel-mar | 727 | |
386 | daniel-mar | 728 | for (i = 0; i < 8; i++) { |
550 | daniel-mar | 729 | if (gdata->parm.ctl_used[i]) { |
730 | int v; |
||
731 | char keyname[6/*strlen("ctl[X]")*/ + 1/*strlen("\0")*/], tmp[5]; |
||
537 | daniel-mar | 732 | |
550 | daniel-mar | 733 | // Slider names |
734 | sprintf(keyname, "ctl[%d]", i); |
||
735 | _picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false); |
||
385 | daniel-mar | 736 | |
550 | daniel-mar | 737 | // Slider values |
738 | sprintf(keyname, "val[%d]", i); |
||
385 | daniel-mar | 739 | if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) { |
550 | daniel-mar | 740 | sprintf(keyname, "def[%d]", i); |
741 | if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) { |
||
742 | strcpy(tmp, "0"); |
||
743 | } |
||
385 | daniel-mar | 744 | } |
550 | daniel-mar | 745 | v = atoi(tmp); |
746 | if (v < 0) v = 0; |
||
747 | else if (v > 255) v = 255; |
||
748 | gdata->parm.val[i] = gdata->parm.val[i] = (uint8_t)v; |
||
385 | daniel-mar | 749 | } |
750 | } |
||
751 | |||
752 | // Map names |
||
386 | daniel-mar | 753 | for (i = 0; i < 4; i++) { |
550 | daniel-mar | 754 | if (gdata->parm.map_used[i]) { |
755 | char keyname[6/*strlen("map[X]")*/ + 1/*strlen("\0")*/]; |
||
756 | sprintf(keyname, "map[%d]", i); |
||
757 | _picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false); |
||
758 | } |
||
385 | daniel-mar | 759 | } |
760 | |||
552 | daniel-mar | 761 | res = 0; |
385 | daniel-mar | 762 | } |
763 | |||
764 | PIUNLOCKHANDLE(h); |
||
765 | PIDISPOSEHANDLE(h); |
||
766 | } |
||
767 | FSClose(refnum); |
||
768 | } |
||
769 | |||
770 | return res; |
||
771 | } |
||
772 | |||
539 | daniel-mar | 773 | Boolean _gufReadProperty(char* fileContents, size_t argMaxInputLength, const char* section, const char* keyname, char* argOutput, size_t argMaxOutLength) { |
774 | size_t iTmp; |
||
775 | char* tmpFileContents, * tmpSection, * tmpStart, * tmpStart2, * tmpEnd, * tmpKeyname, * tmpStart3, * tmpStart4, * inputwork; |
||
776 | |||
777 | // Check parameters |
||
778 | if (argMaxOutLength == 0) return false; |
||
779 | if (fileContents == NULL) return false; |
||
780 | |||
781 | // Handle argMaxInputLength |
||
782 | //char* inputwork = fileContents; |
||
541 | daniel-mar | 783 | inputwork = (char*)malloc((size_t)argMaxInputLength + 1/*NUL byte*/); |
539 | daniel-mar | 784 | if (inputwork == NULL) return false; |
785 | memcpy(inputwork, fileContents, argMaxInputLength); |
||
786 | inputwork[argMaxInputLength] = '\0'; |
||
787 | |||
788 | // Prepare the input file contents to make it easier parse-able |
||
789 | iTmp = strlen(inputwork) + strlen("\n\n["); |
||
541 | daniel-mar | 790 | tmpFileContents = (char*)malloc(iTmp + 1/*NUL byte*/); |
539 | daniel-mar | 791 | if (tmpFileContents == NULL) return false; |
792 | sprintf(tmpFileContents, "\n%s\n[", inputwork); |
||
793 | for (iTmp = 0; iTmp < strlen(tmpFileContents); iTmp++) { |
||
794 | if (tmpFileContents[iTmp] == '\r') tmpFileContents[iTmp] = '\n'; |
||
795 | } |
||
796 | |||
797 | // Find the section begin |
||
798 | iTmp = strlen(section) + strlen("\n[]\n"); |
||
541 | daniel-mar | 799 | tmpSection = (char*)malloc(iTmp + 1/*NUL byte*/); |
539 | daniel-mar | 800 | if (tmpSection == NULL) return false; |
801 | sprintf(tmpSection, "\n[%s]\n", section); |
||
802 | tmpStart = strstr(tmpFileContents, tmpSection); |
||
803 | if (tmpStart == NULL) return false; |
||
804 | tmpStart += iTmp; |
||
805 | |||
806 | // Find the end of the section and set a NULL terminator to it |
||
807 | iTmp = strlen(tmpStart) + strlen("\n"); |
||
808 | tmpStart2 = (char*)malloc(iTmp + 1/*NUL byte*/); |
||
809 | if (tmpStart2 == NULL) return false; |
||
810 | sprintf(tmpStart2, "\n%s", tmpStart); |
||
811 | tmpEnd = strstr(tmpStart2, "\n["); |
||
812 | if (tmpEnd == NULL) return false; |
||
813 | tmpEnd[0] = '\0'; |
||
814 | |||
815 | // Find the start of the value |
||
816 | iTmp = strlen(keyname) + strlen("\n="); |
||
817 | tmpKeyname = (char*)malloc(iTmp + 1/*NUL byte*/); |
||
818 | if (tmpKeyname == NULL) return false; |
||
819 | sprintf(tmpKeyname, "\n%s=", keyname); |
||
820 | tmpStart3 = strstr(tmpStart2, tmpKeyname); |
||
821 | if (tmpStart3 == NULL) return false; |
||
822 | tmpStart3 += strlen("\n"); |
||
823 | tmpStart4 = strstr(tmpStart3, "="); |
||
824 | if (tmpStart4 == NULL) return false; |
||
825 | tmpStart4 += strlen("="); |
||
826 | |||
827 | // Find the end of the value |
||
828 | tmpEnd = strstr(tmpStart4, "\n"); |
||
829 | if (tmpEnd == NULL) return false; |
||
830 | tmpEnd[0] = '\0'; |
||
831 | |||
832 | // Copy to output |
||
833 | if (strlen(tmpStart4) < argMaxOutLength + 1) argMaxOutLength = strlen(tmpStart4) + 1; |
||
834 | memcpy(argOutput, tmpStart4, argMaxOutLength); |
||
835 | argOutput[argMaxOutLength - 1] = '\0'; |
||
836 | |||
837 | // Free all temporary stuff |
||
838 | //for (iTmp = 0; iTmp < strlen(tmpFileContents); iTmp++) tmpFileContents[iTmp] = '\0'; |
||
839 | free(tmpFileContents); |
||
840 | //for (iTmp = 0; iTmp < strlen(tmpSection); iTmp++) tmpSection[iTmp] = '\0'; |
||
841 | free(tmpSection); |
||
842 | //for (iTmp = 0; iTmp < strlen(tmpKeyname); iTmp++) tmpKeyname[iTmp] = '\0'; |
||
843 | free(tmpKeyname); |
||
844 | //for (iTmp = 0; iTmp < strlen(tmpStart2); iTmp++) tmpStart2[iTmp] = '\0'; |
||
845 | free(tmpStart2); |
||
846 | |||
847 | // Return success |
||
848 | return true; |
||
537 | daniel-mar | 849 | } |
850 | |||
552 | daniel-mar | 851 | FFLoadingResult readfile_guf(StandardFileReply* sfr) { |
537 | daniel-mar | 852 | Handle h; |
552 | daniel-mar | 853 | FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
537 | daniel-mar | 854 | FILEREF refnum; |
855 | |||
541 | daniel-mar | 856 | // TODO: Decode UTF-8 to ANSI (or "?" for unknown characters) |
857 | |||
552 | daniel-mar | 858 | if (!fileHasExtension(sfr, TEXT(".guf"))) return MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
537 | daniel-mar | 859 | |
860 | if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) { |
||
861 | if ((h = readfileintohandle(refnum))) { |
||
862 | FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h); |
||
863 | char* q = PILOCKHANDLE(h, false); |
||
553 | daniel-mar | 864 | char protocol[256]; |
537 | daniel-mar | 865 | |
553 | daniel-mar | 866 | if (!_gufReadProperty(q, count, "GUF", "Protocol", protocol, sizeof(protocol))) { |
867 | res = MSG_INVALID_FILE_SIGNATURE_ID; |
||
537 | daniel-mar | 868 | } |
553 | daniel-mar | 869 | else if (strcmp(protocol, "1") != 0) { |
870 | res = MSG_INCOMPATIBLE_GUF_FILE_ID; |
||
871 | } |
||
537 | daniel-mar | 872 | else { |
873 | int i; |
||
541 | daniel-mar | 874 | char tmp[256]; |
875 | char* tmp2; |
||
537 | daniel-mar | 876 | |
877 | // Plugin infos |
||
878 | _gufReadProperty(q, count, "Info", "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle)); |
||
541 | daniel-mar | 879 | _gufReadProperty(q, count, "Info", "Category", tmp, sizeof(tmp)); |
880 | tmp2 = strrchr(tmp, '/'); |
||
881 | if (tmp2 == NULL) { |
||
882 | strcpy(gdata->parm.szCategory, tmp); |
||
883 | } else { |
||
884 | strcpy(gdata->parm.szCategory, tmp2+1); |
||
885 | } |
||
537 | daniel-mar | 886 | _gufReadProperty(q, count, "Info", "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor)); |
887 | _gufReadProperty(q, count, "Info", "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright)); |
||
888 | //_gufReadProperty(q, count, "Filter Factory", "8bf", gdata->parm.xxx, sizeof(gdata->parm.xxx)); |
||
889 | |||
890 | // Expressions |
||
891 | if (!_gufReadProperty(q, count, "Code", "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]))) |
||
892 | strcpy(gdata->parm.szFormula[0], "r"); |
||
893 | if (!_gufReadProperty(q, count, "Code", "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]))) |
||
894 | strcpy(gdata->parm.szFormula[1], "g"); |
||
895 | if (!_gufReadProperty(q, count, "Code", "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]))) |
||
896 | strcpy(gdata->parm.szFormula[2], "b"); |
||
897 | if (!_gufReadProperty(q, count, "Code", "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]))) |
||
898 | strcpy(gdata->parm.szFormula[3], "a"); |
||
899 | |||
900 | for (i = 0; i < 8; i++) { |
||
550 | daniel-mar | 901 | if (gdata->parm.ctl_used[i]) { |
902 | int v; |
||
903 | char keyname[9/*strlen("Control X")*/ + 1/*strlen("\0")*/], tmp[5]; |
||
904 | sprintf(keyname, "Control %d", i); |
||
537 | daniel-mar | 905 | |
550 | daniel-mar | 906 | // Slider names |
907 | _gufReadProperty(q, count, keyname, "Label", gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i])); |
||
537 | daniel-mar | 908 | |
550 | daniel-mar | 909 | // Slider values |
910 | if (!_gufReadProperty(q, count, keyname, "Preset", tmp, sizeof(tmp))) { |
||
911 | strcpy(tmp, "0"); |
||
912 | } |
||
913 | v = atoi(tmp); |
||
914 | if (v < 0) v = 0; |
||
915 | else if (v > 255) v = 255; |
||
916 | gdata->parm.val[i] = gdata->parm.val[i] = (uint8_t)v; |
||
537 | daniel-mar | 917 | } |
918 | } |
||
919 | |||
920 | // Map names |
||
921 | for (i = 0; i < 4; i++) { |
||
550 | daniel-mar | 922 | if (gdata->parm.map_used[i]) { |
923 | char keyname[5/*strlen("Map X")*/ + 1/*strlen("\0")*/]; |
||
924 | sprintf(keyname, "Map %d", i); |
||
925 | _gufReadProperty(q, count, keyname, "Label", gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i])); |
||
926 | } |
||
537 | daniel-mar | 927 | } |
928 | |||
552 | daniel-mar | 929 | res = 0; |
537 | daniel-mar | 930 | } |
931 | |||
932 | PIUNLOCKHANDLE(h); |
||
933 | PIDISPOSEHANDLE(h); |
||
934 | } |
||
935 | FSClose(refnum); |
||
936 | } |
||
937 | |||
938 | return res; |
||
939 | } |
||
940 | |||
552 | daniel-mar | 941 | FFLoadingResult readfile_afs_pff(StandardFileReply *sfr){ |
256 | daniel-mar | 942 | FILEREF r; |
943 | Handle h; |
||
552 | daniel-mar | 944 | FFLoadingResult res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
256 | daniel-mar | 945 | |
946 | if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){ |
||
947 | if( (h = readfileintohandle(r)) ){ |
||
553 | daniel-mar | 948 | if( 0 != (res = readparams_afs_pff(h, fileHasExtension(sfr, TEXT(".pff")))) ) { |
949 | // If .afs and .pff files are not recognized, then it is a hard error. |
||
950 | // If a .txt file has no "%RGB1.0" signature, then it is OK and |
||
951 | // we will return MSG_LOADFILE_UNKNOWN_FORMAT_ID to inform the caller that they shall continue trying formats. |
||
952 | if (!fileHasExtension(sfr, TEXT(".afs")) && !fileHasExtension(sfr, TEXT(".pff"))) { |
||
953 | if (res == MSG_INVALID_FILE_SIGNATURE_ID) res = MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
||
256 | daniel-mar | 954 | } |
955 | } |
||
956 | |||
957 | PIDISPOSEHANDLE(h); |
||
958 | } |
||
959 | FSClose(r); |
||
492 | daniel-mar | 960 | } |
961 | else |
||
552 | daniel-mar | 962 | res = MSG_CANNOT_OPEN_FILE_ID; |
256 | daniel-mar | 963 | |
964 | return res; |
||
965 | } |
||
544 | daniel-mar | 966 | |
552 | daniel-mar | 967 | FFLoadingResult readfile_ffl(StandardFileReply* sfr) { |
544 | daniel-mar | 968 | FILEREF rTmp, refnum; |
969 | Handle h, hTmp; |
||
553 | daniel-mar | 970 | FFLoadingResult res = 0; |
544 | daniel-mar | 971 | StandardFileReply sfrTmp; |
972 | OSErr e; |
||
973 | char* p, * start; |
||
974 | size_t est; |
||
553 | daniel-mar | 975 | int foundFilters = 0; |
544 | daniel-mar | 976 | |
553 | daniel-mar | 977 | // TODO: sollte von außerhalb gesteuert werden [auch bei anderen] |
552 | daniel-mar | 978 | if (!fileHasExtension(sfr, TEXT(".ffl"))) return MSG_LOADFILE_UNKNOWN_FORMAT_ID; |
544 | daniel-mar | 979 | |
980 | if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) { |
||
981 | if ((h = readfileintohandle(refnum))) { |
||
982 | FILECOUNT count = (FILECOUNT)PIGETHANDLESIZE(h); |
||
983 | char* q = PILOCKHANDLE(h, false); |
||
984 | |||
547 | daniel-mar | 985 | char* q2, * tmp_cur_filter_str[29], *token; |
544 | daniel-mar | 986 | int lineNumber = 0; |
987 | int countFilters = 0; |
||
547 | daniel-mar | 988 | |
989 | // This is required to make strtok work, because q is not zero-terminated... |
||
990 | q2 = (char*)malloc(count + 1/*NUL byte*/); |
||
991 | if (q2 == NULL) { |
||
553 | daniel-mar | 992 | res = MSG_OUT_OF_MEMORY_ID; |
547 | daniel-mar | 993 | } |
553 | daniel-mar | 994 | else { |
995 | memcpy(q2, q, count); |
||
996 | q2[count] = '\0'; |
||
547 | daniel-mar | 997 | |
553 | daniel-mar | 998 | token = strtok(q2, "\n"); |
999 | while (token != NULL) { |
||
1000 | size_t i; |
||
1001 | char* token2 = my_strdup(token); |
||
1002 | for (i = 0; i < strlen(token2); i++) { |
||
1003 | if (token2[i] == '\r') token2[i] = '\0'; |
||
544 | daniel-mar | 1004 | } |
553 | daniel-mar | 1005 | if (lineNumber == 0) { |
1006 | if (strcmp(token2, "FFL1.0") != 0) { |
||
1007 | res = MSG_INVALID_FILE_SIGNATURE_ID; |
||
1008 | break; |
||
1009 | } |
||
1010 | } |
||
1011 | else if (lineNumber == 1) { |
||
1012 | countFilters = atoi(token2); |
||
1013 | } |
||
1014 | else |
||
1015 | { |
||
1016 | /* |
||
1017 | * 0 filter_file1.8bf |
||
1018 | * 1 Category 1 here |
||
1019 | * 2 Filter Name 1 here |
||
1020 | * 3 Autor 1 here |
||
1021 | * 4 Copyright 1 here |
||
1022 | * 5 Map1 name or empty line to disable map |
||
1023 | * 6 Map2 name or empty line to disable map |
||
1024 | * 7 Map3 name or empty line to disable map |
||
1025 | * 8 Map4 name or empty line to disable map |
||
1026 | * 9 Slider 1 |
||
1027 | * 10 Slider 2 |
||
1028 | * 11 Slider 3 |
||
1029 | * 12 Slider 4 |
||
1030 | * 13 Slider 5 |
||
1031 | * 14 Slider 6 |
||
1032 | * 15 Slider 7 |
||
1033 | * 16 Slider 8 |
||
1034 | * 17 100 |
||
1035 | * 18 110 |
||
1036 | * 19 120 |
||
1037 | * 20 130 |
||
1038 | * 21 140 |
||
1039 | * 22 150 |
||
1040 | * 23 160 |
||
1041 | * 24 170 |
||
1042 | * 25 r |
||
1043 | * 26 g |
||
1044 | * 27 b |
||
1045 | * 28 a |
||
1046 | */ |
||
1047 | int filterLineNumber = (lineNumber - 2) % 29; |
||
1048 | tmp_cur_filter_str[filterLineNumber] = token2; |
||
1049 | if (filterLineNumber == 28) { |
||
1050 | TCHAR* curFileNameOrig; |
||
1051 | TCHAR* curFileName; |
||
544 | daniel-mar | 1052 | |
553 | daniel-mar | 1053 | curFileNameOrig = curFileName = (TCHAR*)malloc(MAX_PATH * sizeof(TCHAR)); |
544 | daniel-mar | 1054 | |
553 | daniel-mar | 1055 | strcpy_advance(&curFileName, sfr->sfFile.szName); |
1056 | curFileName -= 4; // remove ".ffl" extension |
||
1057 | *curFileName = (TCHAR)0; |
||
544 | daniel-mar | 1058 | |
553 | daniel-mar | 1059 | xstrcat(curFileNameOrig, TEXT("__")); |
1060 | curFileName += strlen("__"); |
||
544 | daniel-mar | 1061 | |
553 | daniel-mar | 1062 | strcpy_advance_a(&curFileName, tmp_cur_filter_str[0]); |
1063 | curFileName -= 4; // remove ".8bf" extension |
||
1064 | *curFileName = (TCHAR)0; |
||
544 | daniel-mar | 1065 | |
553 | daniel-mar | 1066 | #ifdef WIN_ENV |
1067 | xstrcat(curFileNameOrig, TEXT(".txt")); |
||
1068 | #endif |
||
544 | daniel-mar | 1069 | |
553 | daniel-mar | 1070 | sfrTmp.sfGood = true; |
1071 | sfrTmp.sfReplacing = true; |
||
1072 | sfrTmp.sfType = TEXT_FILETYPE; |
||
1073 | xstrcpy(sfrTmp.sfFile.szName, curFileNameOrig); |
||
1074 | #ifdef WIN_ENV |
||
1075 | sfrTmp.nFileExtension = (WORD)(xstrlen(curFileNameOrig) - strlen(".txt") + 1); |
||
1076 | #endif |
||
1077 | sfrTmp.sfScript = smSystemScript; |
||
544 | daniel-mar | 1078 | |
553 | daniel-mar | 1079 | est = 16000; |
544 | daniel-mar | 1080 | |
553 | daniel-mar | 1081 | FSpDelete(&sfrTmp.sfFile); |
1082 | if (FSpCreate(&sfrTmp.sfFile, SIG_SIMPLETEXT, TEXT_FILETYPE, sfr->sfScript) == noErr) |
||
1083 | if (FSpOpenDF(&sfrTmp.sfFile, fsWrPerm, &rTmp) == noErr) { |
||
1084 | if ((hTmp = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that. |
||
1085 | PIUNLOCKHANDLE(hTmp); // should not be necessary |
||
1086 | if (!(e = PISETHANDLESIZE(hTmp, (int32)(est))) && (p = start = PILOCKHANDLE(hTmp, false))) { |
||
544 | daniel-mar | 1087 | |
553 | daniel-mar | 1088 | p += sprintf(p, "Category: %s\n", tmp_cur_filter_str[1]); |
1089 | p += sprintf(p, "Title: %s\n", tmp_cur_filter_str[2]); |
||
1090 | p += sprintf(p, "Copyright: %s\n", tmp_cur_filter_str[4]); |
||
1091 | p += sprintf(p, "Author: %s\n", tmp_cur_filter_str[3]); |
||
1092 | p += sprintf(p, "Filename: %s\n", tmp_cur_filter_str[0]); |
||
1093 | p += sprintf(p, "\n"); |
||
1094 | p += sprintf(p, "R:\n"); |
||
1095 | p += sprintf(p, "%s\n", tmp_cur_filter_str[25]); |
||
1096 | p += sprintf(p, "\n"); |
||
1097 | p += sprintf(p, "G:\n"); |
||
1098 | p += sprintf(p, "%s\n", tmp_cur_filter_str[26]); |
||
1099 | p += sprintf(p, "\n"); |
||
1100 | p += sprintf(p, "B:\n"); |
||
1101 | p += sprintf(p, "%s\n", tmp_cur_filter_str[27]); |
||
1102 | p += sprintf(p, "\n"); |
||
1103 | p += sprintf(p, "A:\n"); |
||
1104 | p += sprintf(p, "%s\n", tmp_cur_filter_str[28]); |
||
1105 | p += sprintf(p, "\n"); |
||
1106 | |||
1107 | // Maps |
||
1108 | for (i = 0; i < 4; i++) { |
||
1109 | char* tmp = tmp_cur_filter_str[5 + i]; |
||
1110 | if (strcmp(tmp, "") != 0) { |
||
1111 | p += sprintf(p, "map[%d]: %s\n", (int)i, tmp_cur_filter_str[5 + i]); |
||
1112 | } |
||
544 | daniel-mar | 1113 | } |
553 | daniel-mar | 1114 | p += sprintf(p, "\n"); |
544 | daniel-mar | 1115 | |
553 | daniel-mar | 1116 | // Controls |
1117 | for (i = 0; i < 8; i++) { |
||
1118 | char* tmp = tmp_cur_filter_str[9 + i]; |
||
1119 | if (strcmp(tmp, "") != 0) { |
||
1120 | p += sprintf(p, "ctl[%d]: %s\n", (int)i, tmp_cur_filter_str[9 + i]); |
||
1121 | } |
||
1122 | tmp = tmp_cur_filter_str[17 + i]; |
||
1123 | if (strcmp(tmp, "") != 0) { |
||
1124 | p += sprintf(p, "val[%d]: %s\n", (int)i, tmp_cur_filter_str[17 + i]); |
||
1125 | } |
||
544 | daniel-mar | 1126 | } |
553 | daniel-mar | 1127 | p += sprintf(p, "\n"); |
1128 | |||
1129 | PIUNLOCKHANDLE(hTmp); |
||
1130 | e = PISETHANDLESIZE(hTmp, (int32)(p - start)); // could ignore this error, maybe |
||
544 | daniel-mar | 1131 | } |
553 | daniel-mar | 1132 | savehandleintofile(hTmp, rTmp); |
1133 | PIDISPOSEHANDLE(hTmp); |
||
544 | daniel-mar | 1134 | } |
553 | daniel-mar | 1135 | FSClose(rTmp); |
544 | daniel-mar | 1136 | } |
553 | daniel-mar | 1137 | |
1138 | free(curFileNameOrig); |
||
1139 | for (i = 0; i < 29; i++) { |
||
1140 | free(tmp_cur_filter_str[i]); // free all "token2" |
||
544 | daniel-mar | 1141 | } |
1142 | } |
||
1143 | } |
||
553 | daniel-mar | 1144 | lineNumber++; |
1145 | token = strtok(NULL, "\n"); |
||
544 | daniel-mar | 1146 | } |
1147 | } |
||
1148 | |||
547 | daniel-mar | 1149 | free(q2); |
1150 | |||
544 | daniel-mar | 1151 | PIUNLOCKHANDLE(h); |
1152 | PIDISPOSEHANDLE(h); |
||
1153 | } |
||
1154 | FSClose(refnum); |
||
1155 | } |
||
1156 | |||
553 | daniel-mar | 1157 | if (res == 0) { |
1158 | res = foundFilters == 0 ? MSG_FFL_NO_FILTERS_DETECTED_ID : MSG_FFL_CONVERTED_ID; |
||
1159 | } |
||
1160 | return res; |
||
544 | daniel-mar | 1161 | } |