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